3 ms·
There is no such thing as a Go variable with no type. They all have a type (could be interface{}) and a value (could be nil). package main import
by shurcooL 13y ago
There is no such thing as a Go variable with no type. They all have a type (could be interface{}) and a value (could be nil).
package main
import (
"fmt"
"github.com/davecgh/go-spew/spew"
)
func main() {
var x interface{}
var y chan (int)
spew.Dump(x)
spew.Dump(y)
var z interface{} = y
spew.Dump(z)
fmt.Println(x == z)
fmt.Println(y == z)
}
// Output:
(interface {}) <nil>
(chan int) <nil>
(chan int) <nil>
false
true
- frou_dh 13y agoDoesn't an interface value have both a static type and a dynamic type and in a case like x its dynamic type is "nothing"? I didn't mean that this is necessarily bad, just that it seems weird for "nil" to be overloaded beyond pointers.
- shurcooL 13y agoYeah. From the Go spec: "The static type (or just type) of a variable is the type defined by its declaration. Variables of interface type also have a distinct dynamic type, which is the actual type of the value stored in the variable at run time. The dynamic type may vary during execution but is always assignable to the static type of the interface variable. For non-interface types, the dynamic type is always the static type."