Golang net/url.Parse() and ParseRequestURI() functions example
package net/url
Golang net/url.Parse() and ParseRequestURI() functions usage example
package main
import (
"fmt"
"net/url"
)
func main() {
rawURL := "http://username:password@searchengine.com:8080/testpath/?q=socketloop.com#TestFunc"
parsedURL, err := url.Parse(rawURL)
if err != nil {
panic(err)
}
fmt.Println(parsedURL)
fmt.Println("Fragment : ", parsedURL.Fragment)
parsedURI, err := url.ParseRequestURI(rawURL)
if err != nil {
panic(err)
}
fmt.Println(parsedURI)
fmt.Println("Fragment : ", parsedURI.Fragment) // see the diff? no fragment for ParseRequestURI
}
Output :
http://username:password@searchengine.com:8080/testpath/?q=socketloop.com#TestFunc
Fragment : TestFunc
http://username:password@searchengine.com:8080/testpath/?q=socketloop.com#TestFunc
Fragment :
References :
http://golang.org/pkg/net/url/#Parse
http://golang.org/pkg/net/url/#ParseRequestURI
https://www.socketloop.com/tutorials/golang-parsing-or-breaking-down-url
Advertisement
Something interesting
Tutorials
+10.6k Golang : ISO8601 Duration Parser example
+36.5k Golang : Save image to PNG, JPEG or GIF format.
+7k Golang : constant 20013 overflows byte error message
+12.7k Golang : Remove or trim extra comma from CSV
+23.1k Golang : Randomly pick an item from a slice/array example
+5.1k Golang : Check if a word is countable or not
+9k Golang : Capture text return from exec function example
+5.7k Golang : Struct field tags and what is their purpose?
+12.9k Golang : Convert IPv4 address to packed 32-bit binary format
+7.3k Golang : Not able to grep log.Println() output
+26.7k Golang : How to check if a connection to database is still alive ?
+14.5k Golang : How to check if your program is running in a terminal