funcmain() { names := []string{ "1", "2", "3", } name := names[len(names)] //应该改为 names[len(names) - 1] fmt.Printf("name: %s",name) //panic: runtime error: index out of range [3] with length 3 }
空指针调用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
package main
type User struct { Name string }
func(u *User)GetName() string { return u.Name }
funcmain() { s := &User{Name: "Neo"} s = nil s.GetName() //panic: runtime error: invalid memory address or nil pointer dereference }