Go Proverbs
Congratulations, you've made it to the last chapter! Before the final exercises, let's meet the Go Proverbs, a collection of short sayings Rob Pike (one of Go's designers) shared in a 2015 talk. They aren't compiler rules; they're a way of thinking that shapes how Go code is written. You can read the full list at go-proverbs.github.io.
Here are a few of them, with explanations and examples drawn from what you've already learned.
1. "Clear is better than clever."
Code is read far more often than it's written. A one-line trick that makes you feel smart today can leave a teammate (or you, six months from now) scratching their head.
// "Cerdik" tapi perlu dipikir dua kali:
diskon := map[bool]int{true: 10, false: 0}[member]
// Jelas:
diskon := 0
if member {
diskon = 10
}
The first version is "clever" but makes you think twice; the second is longer, but its intent is obvious at a glance. Go deliberately has no ternary ?: operator for the same reason.
2. "Errors are values."
In Go, errors are not a special mechanism like exceptions. An error is just an ordinary value that satisfies the error interface. That means you can treat it like any other value: store it in a variable, compare it with errors.Is, wrap it with %w, put it in a struct, or collect it in a slice.
var ErrStokHabis = errors.New("stok habis")
type ValidasiError struct {
Field string
}
func (e *ValidasiError) Error() string { return e.Field + " wajib diisi" }
A related proverb from the same list reminds us: don't just check an error and pass it along as is. Handle it, by adding context or making the right decision.
3. "Make the zero value useful."
Design your types so their zero value is ready to use without a constructor. You've already seen plenty of examples:
var mu sync.Mutex: ready to lock.var sb strings.Builder: ready to write to.var s Stack[int]from the Generics chapter: ready toPush, because you canappendto anilslice.
On the other hand, if the zero value isn't meaningful, make that obvious, like the PeranTidakDikenal = 0 pattern in the Enums chapter.
4. "A little copying is better than a little dependency."
Adding a third-party library for one small function means you take on the whole library: version upgrades, API changes, potential security holes, and build time. If all you need is a few lines, such as the Unik or Berisi functions you wrote in the Generics chapter, copying or writing them yourself is often the healthier choice.
This isn't a ban on dependencies; it's an invitation to weigh the cost.
5. "Don't communicate by sharing memory, share memory by communicating."
Instead of having lots of goroutines fight over the same variable and then lock it everywhere, it's often cleaner to send the data through a channel, so that at any moment only one goroutine "owns" it.
hasil := make(chan int)
for _, url := range daftarURL {
go func() { hasil <- ukuranHalaman(url) }()
}
total := 0
for range daftarURL {
total += <-hasil // hanya goroutine ini yang menyentuh total
}
Another proverb on the same list completes the picture: channels orchestrate; mutexes serialize. As you saw in the Mutexes chapter, a mutex is still the right tool for guarding simple shared state.
6. "The bigger the interface, the weaker the abstraction."
Small interfaces (one or two methods) like io.Reader, io.Writer, fmt.Stringer, and error can be satisfied by a huge number of types, so functions that accept them are very flexible. An interface with twenty methods only fits one or two implementations and is hard to mock in tests.
// Kecil dan kuat:
type Penyimpan interface {
Simpan(kunci string, data []byte) error
}
More proverbs
The full list also includes proverbs about gofmt, interface{}, reflection, unsafe, cgo, documentation, and panic. Read them at go-proverbs.github.io. You now know enough to understand most of them.
Answer the quiz in the panel on the right to test your understanding.