为什么端口是字符串而不是整数?

Port方法返回字符串而不是整数。这样做的原因是什么,以“ :”作为端口的前缀是否安全?

9690 次浏览

This is because what appears after the colon can be either a port number or service name.

The following is a valid example where the service name is used instead of a port number:

listener, err := net.Listen("tcp", "localhost:https") // port 443
// ...

The port mapping for a particular service can be looked up using net.LookupPort. On Unix systems, the /etc/services file is sourced for the mappings.

For a number the default value is 0: if a function

 func (u * URL) Port () string

return number instead of sting the port will be 0

Port 0 is a reserved port in TCP/IP networking, meaning that it should not be used in TCP or UDP messages. However, port 0 carries special significance in network programming, particularly Unix socket programming: for requesting system-allocated, dynamic ports.

it is a need for programming in several functions