Golang : How to get URL port?
From the previous tutorial on how to break down a URL string, a reader emailed me wanting to know if there is a way to get the port number directly from the raw URL?
For example :
rawURL := "http://username:password@searchengine.com:8080/testpath/?q=socketloop.com#TestFunc"
and get the port number 8080 with a function.
From my research, there is a function in the net
package ... the net/SplitHostPort()
function is able to do just that, but with the condition that the URL is in the form of "host:port"
, "[host]:port"
or "[ipv6-host%zone]:port"
To use net/SplitHostPort()
function on rawURL will produce this error message :
too many colons in address http://username:password@searchengine.com:8080/testpath/?q=socketloop.com#TestFunc
and therefore it is recommended to parse the rawURL first
// Parse the URL and ensure there are no errors.
url, err := url.Parse(rawURL)
if err != nil {
panic(err)
}
and apply the SplitHostPort() function on url.Host
shost, sport, err := net.SplitHostPort(url.Host)
fmt.Println("Split Host is : ", shost)
fmt.Println("Split Port is : ", sport)
Here's complete code :
package main
import (
"fmt"
"net"
"net/url"
"strings"
)
func main() {
rawURL := "http://username:password@searchengine.com:8080/testpath/?q=socketloop.com#TestFunc"
fmt.Println("URL : ", rawURL)
// Parse the URL and ensure there are no errors.
url, err := url.Parse(rawURL)
if err != nil {
panic(err)
}
// see http://golang.org/pkg/net/url/#URL
// scheme://[userinfo@]host/path[?query][#fragment]
// get the Scheme
fmt.Println("Scheme : ", url.Scheme)
// get the User information
fmt.Println("Username : ", url.User.Username())
password, set := url.User.Password()
fmt.Println("Password : ", password)
fmt.Println("Password set : ", set)
// get the Host
fmt.Println("Raw host : ", url.Host)
// to get the Port number, split the Host
hostport := strings.Split(url.Host, ":")
host := hostport[0]
port := hostport[1]
fmt.Println("Host : ", host)
fmt.Println("Port : ", port)
// or use net.SplitHostPort
// won't work with rawURL string
shost, sport, err := net.SplitHostPort(url.Host)
fmt.Println("Split Host is : ", shost)
fmt.Println("Split Port is : ", sport)
if err != nil {
fmt.Println(err)
}
// get the Path
fmt.Println("Path : ", url.Path)
// get the RawQuery
fmt.Println("Raw Query ", url.RawQuery)
// get the fragment
fmt.Println("Fragment : ", url.Fragment)
}
Output :
URL : http://username:password@searchengine.com:8080/testpath/?q=socketloop.com#TestFunc
Scheme : http
Username : username
Password : password
Password set : true
Raw host : searchengine.com:8080
Host : searchengine.com
Port : 8080
Split Host is : searchengine.com
Split Port is : 8080
Path : /testpath/
Raw Query q=socketloop.com
Fragment : TestFunc
Reference :
See also : Golang : Parsing or breaking down URL
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+15.4k Golang : File path independent of Operating System
+7.9k Golang : HTTP Routing with Goji example
+6.6k Setting $GOPATH environment variable for Unix/Linux and Windows
+14.9k Golang : Get IP addresses of a domain name
+9.5k Golang : Get currencies exchange rates example
+10.9k Golang : List running EC2 instances and descriptions
+6k Golang : Mapping Iban to Dunging alphabets
+7.6k Golang : Convert(cast) []byte to io.Reader type
+10k Golang : How to use if, eq and print properly in html template
+6k Golang : Transform lisp or spinal case to Pascal case example
+9.4k Golang : Flip coin example