Table of contents
1.
Introduction
2.
Go URL Parsing Example
3.
FAQs
4.
Key takeaways
Last Updated: Mar 27, 2024

Go URL Parsing

Author Shivam Verma
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Go language has good support for URL parsing. URL contains a scheme, authentication info, host, port, path, query params, and query fragment. We can use the parsing URL function url.Parse from the net/url package of Golang to parses a given URL and returns the URI object. 

The net/url package has the required functions like Scheme, User, Host, Path, RawQuery, etc.

Go URL Parsing Example

Example

package main
import (
    "fmt"
    "net"
    "net/url"
)
func main(){  
    link:="golang://user:pass@localhost.com:9000/server/page1?k1=v1&k2=v2#X"  
    u,err:=url.Parse(link)  
    if err!=nil{  
       panic(err)  
    }
    fmt.Println(u.Scheme)   
    fmt.Println(u.User)
    fmt.Println(u.User.Username())
    pass, _:=u.User.Password()
    fmt.Println(pass)
    fmt.Println(u.Host)
    host, port, _:=net.SplitHostPort(u.Host)
    fmt.Println(host)
    fmt.Println(port)
    fmt.Println(u.Path)
    fmt.Println(u.Fragment)
    fmt.Println(u.RawQuery)
    m,_:= url.ParseQuery(u.RawQuery)
    fmt.Println(m)
    fmt.Println(m["k2"][0])
}  

Output

golang
user:pass
user
pass
localhost.com:9000
localhost.com
9000
/server/page1
X
k1=v1&k2=v2
map[k1:[v1] k2:[v2]]
v2

In the above code, The u.Scheme returns the Schema of the URL. The 'User' contains all authentication info; we called Username and Password on this for individual values. The 'Host' contains both the hostname and the port if present. We used SplitHostPort to extract them. We also extracted the path using u.path and the fragment after the # using u.fragment. We used 'RawQuery' to get query parameters in a string of k=v format. We also parsed query parameters into a map using 'ParseQuery'. The parsed query param maps are from strings to slices of strings.

FAQs

  1. What does URL parse do in Golang?
    Ans: A URL contains a scheme, authentication info, host, port, path, query params, and query fragment. We can use the parsing URL function url.Parse from the net/url package of Golang to parse a given URL and returns the URI object. 
     
  2. How do We encode URL in Golang?
    Ans: Go provides two functions in the net/url package for encoding different URL parts.
    QueryEscape(): It encodes a string that is placed inside an URL query.
    PathEscape(): It encodes a string that is placed inside an URL path segment.
     
  3. How do We decode URL in Golang?
    Ans: To decode an escaped URL in Golang, We can use the parsing URL function url.Parse from the url package. It parses and decodes all parts of the URL.
     
  4. How can we check if a URL is valid in Golang?
    Ans: We use the ParseRequestURI() function of the url package to validate our URL. The ParseRequestURI() function returns an error if the URL is not valid.

Key takeaways

In this article, we have extensively discussed URL parsing in the Go programming language with an example.

We hope that this blog has helped you enhance your knowledge regarding the Go URL parsing and if you would like to learn more, check out our article on Go Defer. You can read other C language articles by clicking here. Do upvote our blog to help ninjas grow. Happy Coding!

Live masterclass