This works perferctly:
package main
import "fmt"
type Struct struct {
field string
}
func Fn(arg struct{field string}) {
fmt.Println(arg)
}
func main() {
Fn(Struct{"john"})
}
But this gives ./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"})
}
sub/sub.go
package sub
import "fmt"
func Fn(arg struct{field string}) {
fmt.Println(arg)
}
Only change in function call is that Fn(Struct{"john"})
was replaced with sub.Fn(Struct{"john"})
.
Why moving function to another package affects types logic? Links to the docs will be appreciated.