Welcome To Golang By Example

Menu
  • Home
  • Blog
Menu

Parse a URL and extract all the parts in Go (Golang)

Posted on July 23, 2023July 23, 2023 by admin

Table of Contents

  • Overview
  • Program

Overview

net/url package of golang contains a Parse function that can be used to parse a given and return the url instance of the URL struct

https://golang.org/pkg/net/url/#URL

Once the given URL is parsed correctly, then it will return the URI object. We can then access the below information from the URI

  • Scheme
  • User Info
  • Hostname
  • Port
  • Pathname
  • Query Params
  • Fragment

Let’s see a working program for the same:

We will parse the below URL

https://test:[email protected]:8000/tutorials/intro?type=advance&compact=false#history

Then

  • Scheme is HTTPS
  • User Info – username is test and password is abcd123. Username and password are separated by a colon :
  • Hostname is www.golangbyexample.com
  • Port is 8000
  • The path is tutorials/intro
  • Query Params is type=advance and compact=false. They are separated by ampersand
  • fragment is history. It will directly take to the history section within that page. history is an identifier that refers to that section within that page

Program

package main
import (
"fmt"
"log"
"net/url"
)
func main() {
input_url := "https://test:[email protected]:8000/tutorials/intro?type=advance&compact=false#history"
u, err := url.Parse(input_url)
if err != nil {
log.Fatal(err)
}
fmt.Println(u.Scheme)
fmt.Println(u.User)
fmt.Println(u.Hostname())
fmt.Println(u.Port())
fmt.Println(u.Path)
fmt.Println(u.RawQuery)
fmt.Println(u.Fragment)
fmt.Println(u.String())
}

Output

https
test:abcd123
golangbyexample.com
8000
/tutorials/intro
type=advance&compact=false
history
https://test:[email protected]:8000/tutorials/intro?type=advance&compact=false#history

It correctly dumps all the information as seen from the output

  • go
  • golang
  • Popular Articles

    Golang Comprehensive Tutorial Series

    All Design Patterns in Go (Golang)

    Slice in golang

    Variables in Go (Golang) – Complete Guide

    OOP: Inheritance in GOLANG complete guide

    Using Context Package in GO (Golang) – Complete Guide

    All data types in Golang with examples

    Understanding time and date in Go (Golang) – Complete Guide

    ©2023 Welcome To Golang By Example | Design: Web XP