mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
net/url: document and add example for ParseQuery("x")
Fixes #16460. Change-Id: Ie9d5f725d2d7e8210ab6f7604a5a05fc49f707de Reviewed-on: https://go-review.googlesource.com/31331 Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
parent
ac1108bdcb
commit
59dae58174
2 changed files with 23 additions and 0 deletions
|
|
@ -5,6 +5,7 @@
|
|||
package url_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
|
|
@ -98,3 +99,21 @@ func ExampleURL_ResolveReference() {
|
|||
// Output:
|
||||
// http://example.com/search?q=dotnet
|
||||
}
|
||||
|
||||
func ExampleParseQuery() {
|
||||
m, err := url.ParseQuery(`x=1&y=2&y=3;z`)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Println(toJSON(m))
|
||||
// Output:
|
||||
// {"x":["1"], "y":["2", "3"], "z":[""]}
|
||||
}
|
||||
|
||||
func toJSON(m interface{}) string {
|
||||
js, err := json.Marshal(m)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return strings.Replace(string(js), ",", ", ", -1)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue