On Go package names
Or why I renamed github.com/inkel/go-proxy-protocol
to github.com/inkel/viaproxy
.
In my previous article I introduced a repository that hold the code to create net.Conn
objects aware of the proxy protocol, but I wasn’t happy with the name of the repository.
Package names are important in Go, and one aspect that we tend to overlook is that they actually are part of the calling signature when you want to use an export type or function. With the previous code, if we wanted to use the net.Conn
wrapper we would have to first import the library:
import "github.com/inkel/go-proxy-protocol/conn"
Once we did that, then to wrap a connection we would have to call:
newCn, err := conn.WithProxyProtocol(cn)
Similarly if we wanted to use the net.Listen
alternative, we should’ve had to import github.com/inkel/go-proxy-protocol/listen
and then call cn, err := listen.WithProxyProtocol
. This doesn’t look right to my eyes, and hopefully not to yours either. And aside aesthetics, two packages for such limited code? Doesn’t make much sense.
So I spent the day thinking on a better name that could allow me to better convey the effect we want to achieve and that fits in just one library, and thus, github.com/inkel/viaproxy
came to be. Let’s see how better the code would look like now when wrapping a connection:
// import the package
import "github.com/inkel/viaproxy"
// wrap the connection
newCn, err := viaproxy.Wrap(cn)
Similarly if you want to use the net.Listener
, the code looks just as well (and I might even add that looks better):
// import the package
import "github.com/inkel/viaproxy"
// create the listener
ln, err := viaproxy.Listen("tcp", ":1234")
It certainly looks much better, and I hope you agree.