这完美地工作:
package main
import "fmt"
type Struct struct {
field string
}
func Fn(arg struct{field string}) {
fmt.Println(arg)
}
func main() {
Fn(Struct{"john"})
}
但这给出了./main.go:12: cannot use Struct literal (type Struct) as type struct { field string } in argument to sub.Fn:
main.go
package main
import "go_tests/sub"
type Struct struct {
field string
}
func main() {
sub.Fn(Struct{"john"})
}
子/子.go
package sub
import "fmt"
func Fn(arg struct{field string}) {
fmt.Println(arg)
}
函数调用的唯一变化是Fn(Struct{"john"})被替换为sub.Fn(Struct{"john"}).
为什么将函数移到另一个包会影响类型逻辑?链接到文档将不胜感激。
慕桂英546537
相关分类