来自其他软件包的golang结构

Situation:

I've the following project structure:

root
    parser
       parser.go
    builtin
        exit.go
        hi.go
    structs
        base_structs.go
    main.go

.. and the base_structs.go file looks like this:

package structs

type Built_in_func func([] string)

type Built_in struct {
    s string
    f Built_in_func
}

I've imported the package in my main.go and I'm referencing the struct with structs.Built_in.

This is what I'm trying to do:

var builtin_list [] structs.Built_in

builtin_list = append(builtin_list, structs.Built_in{s:"exit", f:builtin.Exit})
builtin_list = append(builtin_list, structs.Built_in{s:"hi", f:builtin.Hi})

But I'm getting this error:

unknown structs.Built_in field 's' in struct literal

Question:

What am I doing wrong?

In Go, the visibility of a name outside a package is determined by whether its first character is upper case.

So the field s is actually not visible from outside the package structs and you get that error.

If you define your struct like (note the upper case):

type Built_in struct {
    S string
    F Built_in_func
}

Then this will work (again the upper case):

structs.Built_in{S:"exit", F:builtin.Exit}

You can read more here:

https://golang.org/doc/effective_go.html#names