Skip to content

Instantly share code, notes, and snippets.

@charlieegan3
Last active August 17, 2017 14:18
Show Gist options
  • Save charlieegan3/49ae425fbf2d7dcf2b5a414db6c1ea4a to your computer and use it in GitHub Desktop.
Save charlieegan3/49ae425fbf2d7dcf2b5a414db6c1ea4a to your computer and use it in GitHub Desktop.
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