On Wed, Aug 28, 2024 at 6:08 AM Yinebeb Tariku <
yin...@gmail.com> wrote:
>
> I've been diving into the Go standard library's net package and have some questions about the Dialer function and named services.
>
> 1. Where exactly does Go perform the mapping between a service name (e.g., "http") and its corresponding port number (e.g., 80)?
There are two places, depending on whether the program is using the Go
resolver or the cgo resolver (see
https://pkg.go.dev/net for a
discussion of the different resolvers).
On Unix systems, the Go resolver reads the /etc/services file in the
readServices function in net/port_unix.go. It looks up entries in the
goLookupPort function. The Unix /etc/services file has a mapping from
http to port 80 on TCP.
On Unix systems, the cgo resolver calls the C getaddrinfo function.
This happens in cgoLookupServicePort in net/cgo_unix.go. getaddrinfo
knows how to translate strings like "http" into port numbers like 80,
among other things.
> 2. Is there a significant advantage or disadvantage to using actual port numbers directly instead of named services when dialing connections? If so, could you share some insights or point me to relevant resources?
It's probably clearer to future readers to use the service names
rather than the port numbers. I can't think of anything more
significant than that.
Ian