δΎ‹:
package a
func CreateUser(name string) *User {
if isValid(name) {
return &User{Name: name}
}
return nil
}
package main
func main() {
user := CreateUser("NilDereference")
print(user.Name) // Potential nil dereference
}
In the provided example, user is a pointer to a User struct, but it can be nil because CreateUser
may return nil. Attempting to dereference user to access Name without first checking whether user
is nil may result in a runtime error.