A short post, the main value of which will be comments (I hope).
I switched to Go quite recently. So far I have noted three problems:
time.Ticker
does not stop: skipped linedefer tick.Stop()
The commentary on the library type says it is "concurrently safe", but does not say exactly how methods can be called or from which goroutines. The default can be considered "any method of a type can be called from anywhere in any sequence", but in practice this is not the case for most thread-safe types: they either have some life cycle (start-stop), or the semantics only support one writer: mutating methods can be called from only one goroutine, or both.
The point is related and partly overlapping with the previous one: there is a field in the structure
sync.Mutex
, but there is no comment explaining what it protects, and most importantly why (for example, "access to these fields must be protected, because they are written from such and such a goroutine and read from goroutinesnet/http
server ").
Note that even in the golang / go source, which is usually not commented on (apart from the documentary ones), all mutexes in subpackagesnet/
have short comments.
A question for people who have been writing in Go longer - what problems do you regularly notice, during reviews, delving into the old code, or the code of libraries?
This post is a cross- post translation from a post on Reddit .