A host names a machine, but a connection needs a port too. Today you read an explicit port when the URL gives one and fall back to the scheme's default when it does not - 80 for http, 443 for https.
Parse an explicit port from the host, or supply the scheme's default port when none is given.
To open a connection you need a host and a port. The URL may state one
explicitly as host:port - example.com:8080 means port 8080 - and when it
does, the host is just the part before the colon. When the URL gives no port, the
scheme’s default applies: 80 for http, 443 for https. A browser hides
this from you, but a client has to make the default concrete before it can dial.
Pin both cases now: an explicit port must be parsed as a number and split cleanly off the host, and each scheme must resolve to the right default. From here on every URL carries a real port, so nothing downstream has to guess.
// the authority may be "host" or "host:port". if a ":" is present,// the part after it is the port. otherwise pick the default:// http -> 80// https -> 443func defaultPort(scheme string) int {// return 80 or 443 based on the scheme}