What “accept interfaces, return structs” means in Go
Jack Lindamood
3153

Another great reason is better testing. If a function takes a struct, especially one from another package, it’s hard to test the functionality of that function without knowing how to mock that struct, which can require complex setup. Using an interface can solve the problem, allowing the test to specify only the behaviour necessary to satisfy the interface use, rather than fully setting up the struct.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.