patterngoCritical
Why is port a string and not an integer?
Viewed 0 times
portwhyandintegernotstring
Problem
The
Port method returns a string and not an integer. What is the reason for that and is it safe to prefix the port with ":"?Solution
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:
The port mapping for a particular service can be looked up using
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.Code Snippets
listener, err := net.Listen("tcp", "localhost:https") // port 443
// ...Context
Stack Overflow Q#47992477, score: 150
Revisions (0)
No revisions yet.