54
28

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

golangでURLをパース

Last updated at Posted at 2016-06-25

net/urlパッケージを使ってパースする.

parse.go
package main

import (
  "fmt"
  "log"
  "net/url"
)

func main() {
  //General form
  u, err := url.Parse("http://user1@bing.com:8080/search?q=foo%2fbar&q2=hoge#fragment")
  if err != nil {
    log.Fatal(err)
  }
  fmt.Printf("URL: %s\n", u.String())
  fmt.Printf("Scheme: %s\n", u.Scheme)
  fmt.Printf("Opaque: %s\n", u.Opaque)
  fmt.Printf("User: %s\n", u.User)
  fmt.Printf("Host: %s\n", u.Host)
  fmt.Printf("Hostname(): %s\n", u.Hostname())
  fmt.Printf("Path: %s\n", u.Path)
  fmt.Printf("RawPath: %s\n", u.RawPath)
  fmt.Printf("RawQuery: %s\n", u.RawQuery)
  fmt.Printf("Fragment: %s\n", u.Fragment)
  
  for key, values := range u.Query() {
    fmt.Printf("Query Key: %s\n", key)    
    for i, v := range values {
      fmt.Printf("Query Value[%d]: %s\n", i, v)          
    } 
  }  
}
$go run parse.go
URL: http://user1@bing.com/search?q=foo%2fbar&q2=hoge#fragment
Scheme: http
Opaque:
User: user1
Host: bing.com:8080
Hostname(): bing.com
Path: /search
RawPath:
RawQuery: q=foo%2fbar&q2=hoge
Fragment: fragment
Query Key: q
Query Value[0]: foo/bar
Query Key: q2
Query Value[0]: hoge
54
28
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
54
28

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?