It turns out that this is a misunderstanding that I had about how Go relates to types, which can be solved by reading the relevant part of the specification:
http://golang.org/ref/spec#Type_identity
The corresponding distinction that I did not know about applies to types with and without a name .
Named types are types with a name such as int, int64, float, string, bool. In addition, any type that you create using "type" has a named type.
Types without are like [] string, map [string] string, [4] int. They do not have a name, just a description corresponding to how they should be structured.
If you are comparing two named types, the names must match so that they are interchangeable. If you are comparing a named and an unnamed type, then as long as the main view is consistent , you're good to go!
eg. given the following types:
type MyInt int type MyMap map[int]int type MySlice []int type MyFunc func(int)
the following is not allowed:
var i int = 2 var i2 MyInt = 4 i = i2
in order:
is := make([]int) m := make(map[int]int) f := func(i int){}
I'm a bit gutted that I didn't know before, so I hope this clarifies the lark type for someone else! And it means much less casting than I thought at first :)
jsdw Oct 12 '13 at 13:51 2013-10-12 13:51
source share