In Go, nil represents the absence of a usable value for applicable references and interfaces. Passing nil as an interface dependency can compile successfully but fail when the program calls through it. section 6.5.5
Use nil carefully in these situations:
- A nil pointer or interface cannot provide the method or field the program tries to access; the runtime raises a panic for an invalid memory address or nil pointer dereference. section 6.5.5
- Slices cannot be compared with == to other slices, but they can be compared with nil. section 5.1.2
- Tests should replace a nil dependency with a small stub when the code under test calls its methods. section 6.5.5
A nil dependency panics
A conversion test passes nil to money.Convert as its exchange-rate provider: got, err := money.Convert(tc.amount, tc.to, nil). The call compiles, but invoking FetchExchangeRate produces a runtime panic. A stub implementing the interface supplies controlled rate and error values instead. section 6.5.5
got, err := money.Convert(tc.amount, tc.to, nil)
Inspect the minimal nil dependency passed to Convert, then compare it with the resulting runtime failure. section 6.5.5


































