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.
You need to export your struct field:
type Struct struct {
Field string
}
and then also change the call to use the exported field:
func Fn(arg struct{Field string}) {
fmt.Println(arg)
}
From the language spec (specifically the last sentence):
For struct literals the following rules apply:
- A key must be a field name declared in the LiteralType.
- An element list that does not contain any keys must list an element for each struct field in the order in which the fields are declared.
- If any element has a key, every element must have a key.
- An element list that contains keys does not need to have an element for each struct field. Omitted fields get the zero value for that field.
- A literal may omit the element list; such a literal evaluates to the zero value for its type.
- It is an error to specify an element for a non-exported field of a struct belonging to a different package.