Exported fields
A struct field whose name starts with an uppercase letter is exported: other packages can read and write it. A lowercase field is visible only inside the struct's package.
Detailed Explanation
Go has no public or private keywords. The first letter is the rule, and it applies to fields the same way it applies to types and functions. Token string on Session is part of your API. token string is an implementation detail. Importers can write s.Token and cannot write s.token. Files in the same package can use both.
Export is per name, not per struct. You can export the type and hide the fields: type Session struct { token string }. Callers construct sessions through NewSession and read the token through a method. That is how you keep invariants. You can also export some fields and hide others on the same struct.
Encoding follows the same rule. encoding/json and encoding/xml only see exported fields. A lowercase token will not appear in JSON unless you write a custom marshaler. The json struct tag names the key; it does not export the field. json:"token" on an unexported field is still ignored by the default marshaler.
Cross-package literals. Session{Token: "abc"} is legal from another package if Token is exported. Session{token: "abc"} is not. A composite literal that uses positional fields (Session{"abc", 42}) requires every field to be exported, and it breaks when you add a field. Prefer keyed literals, and prefer constructors when any field is unexported.
Getters are not automatic. If you hide a field, you write func (s Session) Token() string yourself when callers need to read it. Go style is to name the method after the field without Get. Do not export a field and a getter for the same data.
What it costs. Exported fields are a stable API. Adding, renaming, or changing the type of Token is a breaking change for every importer and every JSON consumer. Start unexported. Export when callers have a reason to set the field themselves and you do not need to validate the write.
Code Examples
package session
type Session struct {
Token string // exported
user int64 // unexported
}
func New(token string, user int64) Session {
return Session{Token: token, user: user}
}
func (s Session) User() int64 { return s.user }package main
import (
"fmt"
"example.com/shop/session"
)
func main() {
s := session.New("abc", 9)
fmt.Println(s.Token, s.User())
// fmt.Println(s.user) // compile error in this package
}type Row struct {
Name string `json:"name"`
pass string `json:"pass"` // still omitted by encoding/json
}Common Gotchas
JSON tags do not export a field
lowercase fields stay invisible to `encoding/json` no matter the tag.
An exported mutable field is shared state if you pass a pointer
any importer can write it. Hide the field if writes must go through validation.
Positional struct literals cannot set unexported fields from another package
use keys or a constructor.
Tests in `package foo_test` are another package
they cannot see unexported fields. Put those tests in `package foo` if they must.