When a nil of golang is passed into the method, it becomes not nil?. Why is that?

[]
the value is nil
not nil

1. Why is not nil?
2 printed in Console? How to realize that nil? can be printed out in Console (that"s why interface {} is used because of the need for a general method.)

Feb.07,2022

API interface, including two aspects

  • type interface type
  • value API dynamic value

judge that data interface {} = = nil, should satisfy both aspects.
in your example, type is valuable, value = = nil

this corresponds to reflect.TypeOf and reflect.ValueOf methods


is there any other way besides reflection?

func Console(data interface{}) {
    v := reflect.ValueOf(data)
    if(v.IsNil()){
        fmt.Println("is nil")
    }else{
        fmt.Println("not nil")
    }
}
Menu