Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between struct{a int;b int} and struct{b int;a int}?

Tags:

types

go

What is the difference between these two structs other than that they aren't considered equivalent?

package main
func main() {}
func f1(s struct{a int;b int}) { f2(s) }
func f2(s struct{b int;a int}) {}

$ go run a.go 
# command-line-arguments
./a.go:3: cannot use s (type struct { a int; b int }) as type struct { b int; a int } in argument to f2

Note: this does compile:

package main
func main() {}
func f1(s struct{a int;b int}) { f2(s) }
func f2(s struct{a int;b int}) {}
like image 836
Dog Avatar asked Aug 26 '14 14:08

Dog


1 Answers

"The order of structs' fields is important on the low level" How?

This will impact the reflection, like func (v Value) Field(i int) Value:

Field returns the i'th field of the struct v

The first field 'a' in the first structure wouldn't be the same first in the second structure.
That also will influence serialization with marshaler methods (encoding package).

like image 96
VonC Avatar answered Sep 29 '22 17:09

VonC