I'm used to a router where you define a big list of routes all in one place, maybe in some DSL. That's very easy to refactor, and doesn't have any ambiguity. If a library wants a route registered it puts a snippet in the docs for users to copy.
Go in general seems to value clarity over magic at the expense of verbosity, and this is a puzzling exception.
Large code bases are large, and not every program is your simple crud app with a handful of endpoints that can be meaningfully managed a single function.
People's willingness to defend any and all of go's dubious decisions is really baffling.
To me, that's reasonable behavior and is consistent with other things such as https://pkg.go.dev/regexp#MustCompile
I think this pattern in the standard library is a mistake.
import _ "thing"
Vs: import "thing"
thing.Register()
But one uses a strange construct to save a single line, loses the ability to control order, and encourages people to use globals that they can't control.Still, I can understand it for some components like loggers to not add boiler plate to every library. However, I was very uneasy to see that enabling gzip decompression in a gRPC server is done through a magic _ import. You have to initialize the server anyways, so why not just make it an explicit function argument?
From the design proposal[1]:
See also the background discussion[2]: [1]: https://github.com/golang/go/issues/61410 [2]: https://github.com/golang/go/discussions/60227