Let’s start with an example to see if you can spot problems in the code.
package main
import "fmt"
func main() {
arr := []string{"hello", "world"}
fmt.Println(test(arr))
}
func test(arr []string) string {
return arr[0]
}
In real life, you probably wouldn’t write this simple code, but the errors in the above code might occur in your code. As for what is wrong with the above code, the reason is that when referring to a slice that is nil
, even the element with the index of 0 will cause the code to appear index out of range
.
package main
import "fmt"
func main() {
arr := []string{"hello", "world"}
fmt.Println(test(arr))
fmt.Println(test(nil))
}
func test(arr []string) string {
return arr[0]
}
Execute the above code, and a panic will be triggered, causing the program to stop running. When panic occurs in a multi-layered nested function call, the program immediately aborts the execution of the current function, and all defer statements return control to the caller who received the panic. This bubbles up to the top layer, executes defer for each layer, and then crashes the program at the top of the stack.
hello
panic: runtime error: index out of range
goroutine 1 [running]:
main.test(...)
D:/mygo/test.go:12
main.main()
D:/mygo/test.go:8 +0xc0
exit status 2
In a practical application, this situation is not allowed, and there is no panic to stop the program. You can do this by using the recover built-in function to recover the program from panic. Where recover
can only be used in the defer decorated function to get the error value passed in the panic call, if it is executed normally, the call recover returns nil and has no other effect.
package main
import "fmt"
func main() {
arr1 := []string{"hello", "world"}
fmt.Println(test(arr1))
defer func() {
if err := recover(); err != nil {
fmt.Printf("panic %s\n", err)
}
}()
fmt.Println(test(nil))
}
func test(arr []string) string {
return arr[0]
}
Another method is to first judge the parameters in the function containing the slice parameters. Such as:
package main
import "fmt"
func main() {
arr := []string{"hello", "world"}
fmt.Println(test(arr))
fmt.Println(test(nil))
}
func test(arr []string) string {
if len(arr) != 0 {
return arr[0]
}else {
return "error"
}
}
div>
Read More:
- 【Bug-python】IndexError: list index out of range
- String index out of range: 100 error report details and Solutions
- Caused by: java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0
- After the go pointer is declared and assigned, a panic: runtime error: invalid memory address or nil pointer reference appears
- Go start error: Panic: runtime error: invalid memory address or nil pointer reference
- Port out of range: – 1 for Tomcat startup in idea
- On set in pandas_ Index and reset_ Usage of index
- Index error: invalid index to scalar variable
- [Solved] Es delete all the data in the index without deleting the index structure, including curl deletion
- The docker runtime container reported an error: error response from daemon: OCI runtime create failed
- TypeError: unsupported operand type(s) for *: ‘range‘ and ‘int‘
- Elasticsearch6. X invalid time range query bug
- Python error type error: ‘range’ object does not support item assignment, solution
- Max must be larger than min in range parameter
- Reintex index of pandas
- Cause of runtime error on OJ
- Runtime error 5 Invalid procedure call or argument
- ‘ascii‘ codec can‘t decode byte 0x90 in position 614: ordinal not in range(128)
- Matplotlib of data visualization plt.xlim The () ylim() function sets the x-axis and y-axis range coordinates
- UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 0-2: ordinal not in range(128)