Go: Public vs. private
All identifiers defined within a package are visible throughout that package.
When importing a package you can access only its exported identifiers.
An identifier is exported if it begins with a capital letter.
Example
In this package, the only exported identifiers are StopWatch
and Start
:
package timer
import "time"
// A StopWatch is a simple clock utility.
// Its zero value is an idle clock with 0 total time.
type StopWatch struct {
start time.Time
total time.Duration
running bool
}
// Start turns the clock on.
func (s *StopWatch) Start() {
if !s.running {
s.start = time.Now()
s.running = true
}
}
The StopWatch
and its exported methods can be imported and used in a different package:
package main
import "timer"
func main() {
clock := new(timer.StopWatch)
clock.Start()
if clock.running { // illegal
…
}
}
../main.go:8:15: clock.running undefined (cannot refer to unexported field or method clock.running)
Comments
Be the first to comment!