Vote count:
0
I have a function UseLocalType(LocalType) defined in the main package. I can call it with a value of either LocalType or of struct{i int; s string} (the underlying type of LocalType). But when I make a similar function UseOtherType(OtherType) in another package, and try to pass the underlying type of OtherType to it, it no longer compiles. Why?
package m
func MakeOtherType() OtherType { return OtherType{1,2} }
func UseOtherType(OtherType) {}
type OtherType struct {x int; y int}
package main
import "m"
func main() {
UseLocalType(LocalType{1,"A"})
UseLocalType(struct{i int; s string}{1,"A"})
m.UseOtherType(m.MakeOtherType())
//m.UseOtherType(struct{x int; y int}{1,2})
}
func UseLocalType(LocalType) {}
type LocalType struct {i int; s string}
If you uncomment m.UseOtherType(struct{x int; y int}{1,2}), you get this compile error:
$ GOPATH=`pwd` go run a.go
# command-line-arguments
./a.go:7: cannot use struct { x int; y int } literal (type struct { x int; y int }) as type m.OtherType in argument to m.UseOtherType
asked 1 min ago
Structs are no longer equivalent when moved to different package?
Aucun commentaire:
Enregistrer un commentaire