Middle — Senior
Can you give examples of architectural solutions that turned out to be wrong or inefficient, and explain why?
sobes.tech AI
Answer from AI
Erroneous or inefficient architectural decisions in Go are often related to improper application of language principles and patterns:
- Too large monolithic structure. When all code is concentrated in one package or file, it complicates maintenance and testing.
- Lack of interfaces for abstractions. In Go, interfaces allow easy replacement of implementations, but if not used, the code becomes tightly coupled.
- Use of global variables for state. This leads to issues with concurrency and testing.
- Incorrect management of goroutines. For example, launching goroutines without control over their completion or without context, leading to resource leaks.
- Excessive use of channels where simple functions would suffice. Channels are good for synchronization, but improper use complicates the code.
An example of a bad solution is launching a goroutine without context and control:
func process() {
go func() {
// infinite loop without a stop mechanism
for {
// work
}
}()
}
It is better to use context for cancellation:
func process(ctx context.Context) {
go func() {
for {
select {
case <-ctx.Done():
return
default:
// work
}
}
}()
}