To expand on Kevin's answer:

To call a function, the argument must be assignable
<https://golang.org/ref/spec#Assignability> to the parameter type of the
function.
In this case, none of the types involved is a defined type, channel,
interface, pointer…, so the only case left for them to be assignable would
be if the types are identical <https://golang.org/ref/spec#Type_identity>,
and:

Two struct types are identical if they have the same sequence of fields,
> and if corresponding fields have the same names, and identical types, and
> identical tags. *Non-exported field names from different packages are
> always different*.


(emphasis mine)

Therefore, in the case of `b.F(s)` does not work, as the `byte` fields have
different names - they might be written out the same, but as they are from
different packages, they are considered different names. `f(s)` *does*
work, as in that case the field names are from the same package.

reflect <https://pkg.go.dev/reflect#StructField> also exposes this
semantic. For unexported fields (and only for those) it sets the `PkgPath`
as well. So you can think of unexported field names as being implicitly
qualified by their package path.

On Tue, Nov 16, 2021 at 11:51 PM Óscar Carrasco <oxc...@gmail.com> wrote:

> Hello, my issue is similar to:
>
> https://stackoverflow.com/questions/38784963/exporting-functions-with-anonymous-struct-as-a-parameter-cannot-use-value-type
>
> In this particular case, it is fixed by exporting the fields capitalizing
> the field name. But, what if the struct fields are also anonymous?
>
> main.go:
> ----------------------------------------------------
> package main
>
> import "a/b"
>
> func f(s struct{ byte }) {}
>
> func main() {
> s := struct{ byte }{}
> f(s)   // This works
> b.F(s) // This gives an argument type error
> }
> ----------------------------------------------------
>
> b/b.go:
> ----------------------------------------------------
> package b
>
> func F(c struct{ byte }) {
> }
> ----------------------------------------------------
>
> By building this code, we get the following compiler error:
> `cannot use s (type struct { byte }) as type struct { byte } in argument
> to b.F`
>
> In my opinion, it should be allowed to export the unnamed (anonymous)
> types so the struct can be used anywhere else. Opinions on this?
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/dcd6d246-4091-49da-8631-450829e6de68n%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/dcd6d246-4091-49da-8631-450829e6de68n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAEkBMfHPuMG3y0cyE9pOCbDC7xTKLrKC4RiJxOZrarzK-u5%2BMw%40mail.gmail.com.

Reply via email to