HiveBrain v1.2.0
Get Started
← Back to all entries
patterngoCritical

Why is port a string and not an integer?

Submitted by: @import:stackoverflow-api··
0
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:

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.