给定一个端口 return 在 Golang 中对应的服务(例如 http,ssh,...)

Given a port return the corresponding service (e.g. http, ssh, ...) in Golang

我在玩 net 包。虽然我看到许多有用的 Lookup* 函数,但似乎没有任何一个 return 给定端口使用的服务。

例如(这纯属编造):

service, err := net.LookUpService(23)
// service is ssh

我看到这实际上可以在具有 dns.lookupService 的节点中实现,其中提供的回调实际上是为服务提供的。 go 中可能有类似的东西吗?我发现在给定服务和网络(例如 tcp、udp)它 returns 端口的情况下相反,但我有点想要相反的东西。我希望这是有道理的。

在包 netdb 中查找 https://godoc.org/honnef.co/go/netdb

Package netdb provides a Go interface for the protoent and servent structures as defined in netdb.h

A pure Go implementation is used by parsing /etc/protocols and /etc/services

具体来说:

func GetServByPort

func GetServByPort(port int, protocol *Protoent) *Servent

GetServByPort returns the Servent for a given port number and protocol. If the protocol is nil, the first service matching the port number is returned.