Common Interface Mistakes in Go
The article delves into interface mistakes in Go programming, stressing understanding of behavior-driven, concise interfaces. It warns against excessive, non-specific interfaces and offers guidance from industry experts for improvement.
Read original articleThe article discusses common interface mistakes in Go programming. It emphasizes the importance of understanding Go's unique approach to interfaces, focusing on behavior-driven interfaces, avoiding interface pollution, and keeping interfaces small and behavior-specific. The text highlights the pitfalls of creating excessive interfaces, having too many methods in interfaces, returning interfaces instead of concrete structs, creating interfaces solely for testing purposes, and failing to verify interface compliance. By following Go's principles of simplicity, readability, and organic code design, developers can write more effective and idiomatic Go programs. The article provides practical examples and insights from industry experts like Rob Pike to guide programmers in improving their Go skills and avoiding common interface pitfalls.
Related
The software world is destroying itself (2018)
The software development industry faces sustainability challenges like application size growth and performance issues. Emphasizing efficient coding, it urges reevaluation of practices for quality improvement and environmental impact reduction.
Interface Upgrades in Go (2014)
The article delves into Go's interface upgrades, showcasing their role in encapsulation and decoupling. It emphasizes optimizing performance through wider interface casting, with examples from io and net/http libraries. It warns about complexities and advises cautious usage.
Three ways to think about Go channels
Channels in Golang are locked, buffered queues for message passing. They integrate with goroutines, select blocks, and more, offering efficient concurrency. Understanding their role and benefits is crucial for Golang developers.
Elixir Gotchas
The article highlights common pitfalls in Elixir programming, including confusion between charlists and strings, differences in pattern matching, struct behavior, accessing struct fields, handling keyword lists, and unique data type comparisons.
Atomic Operations Composition in Go
The article discusses atomic operations composition in Go, crucial for predictable results in concurrent programming without locks. Examples show both reliable and unpredictable outcomes, cautioning about atomics' limitations compared to mutexes.
The interface cost is real, but I'm not yet convinced the other solutions for simulating errors while under test are not even more costly. testcontainers is a complete non-starter. I'm not, for example, going to physically crash my hard drive to get the appropriate errors out of it.
If you are not documenting your failure cases in your tests, you may as well not write any code at all. The failure states are the most important part of your application.
Don't return an interface? Why would I do that, when I can store it through void ** parameter you pass to me? Ha!
HRESULT QueryInYourFace(
REFIID riid,
void **ppvObject
);
And I won't make a move without designing interfaces. Interfaces is all there is, all the way down!Before we write a line of code, we fill a whiteboard with boxes that have antennas with little balls on the end. Those are interfaces.
> Go is still a new language
Go is between 12 and 17 years old, depending on how you look at it. It's not as old as the big players, but it's far from new.
> Do not think returning an interface is a bad idea
It's almost always a bad idea to return an interface. Return a type that implements said interface(s). If you want to hide away details, use lowercase to not export them. This section had a solid point, then backtracked a little, and I wish it hadn't.
type State[TValue cmp.Ordered, TCost any] interface {
TransitionTo(context Context[TValue, TCost], value TValue) State[TValue, TCost]
... other methods
where Context is also a generic interface. I should add a TContext generic type to State instead of casting it inside my State implementation. It definitely leads to a proliferation of generic types being required on all methods that touch these things. I used to use Go for its terseness. Generics make it much less terse, but I like what they enable.Related
The software world is destroying itself (2018)
The software development industry faces sustainability challenges like application size growth and performance issues. Emphasizing efficient coding, it urges reevaluation of practices for quality improvement and environmental impact reduction.
Interface Upgrades in Go (2014)
The article delves into Go's interface upgrades, showcasing their role in encapsulation and decoupling. It emphasizes optimizing performance through wider interface casting, with examples from io and net/http libraries. It warns about complexities and advises cautious usage.
Three ways to think about Go channels
Channels in Golang are locked, buffered queues for message passing. They integrate with goroutines, select blocks, and more, offering efficient concurrency. Understanding their role and benefits is crucial for Golang developers.
Elixir Gotchas
The article highlights common pitfalls in Elixir programming, including confusion between charlists and strings, differences in pattern matching, struct behavior, accessing struct fields, handling keyword lists, and unique data type comparisons.
Atomic Operations Composition in Go
The article discusses atomic operations composition in Go, crucial for predictable results in concurrent programming without locks. Examples show both reliable and unpredictable outcomes, cautioning about atomics' limitations compared to mutexes.