Golang (* interface {}) (nil) is zero or not?

The code snippet is as follows:

package main

import (
    "fmt"
    "reflect"
)

func main() {
    a := (*interface{})(nil)
    fmt.Println(reflect.TypeOf(a), reflect.ValueOf(a))
    var b interface{} = (*interface{})(nil)
    fmt.Println(reflect.TypeOf(b), reflect.ValueOf(b))
    fmt.Println(a == nil, b == nil)
}

output as below: *interface {} <nil> *interface {} <nil> true false therefore var interface{}different from :=why?

+4
source share
1 answer

according to golang faq

Under covers, interfaces are implemented as two elements, a type and a value. The value called the dynamic value of the interface is an arbitrary concrete value, and the type is a value. For an int 3 value, the interface value contains, schematically, (int, 3).

, , (nil, nil). , nil nil. nil * int , * int : (* int, nil). , , .

a := (*interface{})(nil) var a *interface{} = nil.

var b interface{} = (*interface{})(nil), b - interface{} interface{} nil, nil, , *interface{} nil.

+9

Source: https://habr.com/ru/post/1673369/


All Articles