Visibility
Context
Section titled “Context”In Go, visibility is determined by capitalization. Identifiers that start with an uppercase letter are exported (visible outside the package). Lowercase identifiers are unexported (private to the package).
Example
Section titled “Example”Create a package with both exported and unexported identifiers.
Code example
Section titled “Code example”// person.gopackage person
type Person struct { Name string // exported age int // unexported}
func New(name string, age int) Person { return Person{Name: name, age: age}}
func (p Person) GetAge() int { // exported return p.age}// main.gopackage main
import ( "fmt" "person")
func main() { p := person.New("Alice", 30) fmt.Println(p.Name) // ok // fmt.Println(p.age) // error: age not exported fmt.Println(p.GetAge()) // ok}Output
Section titled “Output”Alice30