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
+21.3k Golang : GORM create record or insert new record into database example
+7.3k Golang : Handling Yes No Quit query input
+7.4k Golang : Mapping Iban to Dunging alphabets
+11.4k Golang : How to detect a server/machine network interface capabilities?
+5.4k Golang : Detect words using using consecutive letters in a given string
+14.8k Golang : Save(pipe) HTTP response into a file
+5.9k Golang : Measure execution time for a function
+11.1k Golang : Post data with url.Values{}
+18.9k Golang : Check if directory exist and create if does not exist
+11.8k Golang : Decompress zlib file example
+13.3k Golang : reCAPTCHA example
+16.8k Golang : Covert map/slice/array to JSON or XML format