Skip to content

Instantly share code, notes, and snippets.

@charlieegan3

charlieegan3/urls.go

Last active Aug 17, 2017
Embed
What would you like to do?
url parsing fun
func main() {
// let's parse some youarells
url1, _ := url.Parse("http://www.example.com")
url2, _ := url.Parse("www.example.com")
// oops, this the second one needs a scheme, let's set one:
url2.Scheme = "http"
// perfect - now my urls are the same
fmt.Println(url1.String()) // => http://www.example.com
fmt.Println(url2.String()) // => http://www.example.com
// but wait...
fmt.Println(url1.Host) // => www.example.com
fmt.Println(url2.Host) // => ""
// hmm - what's the path?
fmt.Println(url1.Path) // => ""
fmt.Println(url2.Path) // => www.example.com, doesn't look like a path...
// what happens if we parse it again...
url2a, _ := url.Parse(url2.String())
fmt.Println(url1.Host) // => www.example.com
fmt.Println(url2a.Host) // => www.example.com
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment