Why is port a string and not an integer?

HttpGoPort

Http Problem Overview


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 ":"?

Http Solutions


Solution 1 - Http

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.

Solution 2 - Http

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser6216224View Question on Stackoverflow
Solution 1 - HttpTim CooperView Answer on Stackoverflow
Solution 2 - HttpFadidView Answer on Stackoverflow