FAQ
This came in a search recently for me, and there is since what I believe a
more idiomatic way of doing this:

For those who want an example to play
with: http://play.golang.org/p/kcBAs--SZy

For those who can't be bothered to click through to another link and would
otherwise just copy paste the code above:

package main

import (
"fmt"
"net/url"
)

func main() {
  // way 1
baseUrl := "http://google.com/search?"
  params := url.Values{}
params.Add("query", "keyword")
  finalUrl := baseUrl + params.Encode()
fmt.Println(finalUrl)
}
On Friday, August 6, 2010 12:39:25 PM UTC-5, Michael Hoisie wrote:

There isn't a library method to do this, but I've used this code
before:

func Urlencode(data map[string]string) string {
var buf bytes.Buffer
for k, v := range data {
buf.WriteString(http.URLEscape(k))
buf.WriteByte('=')
buf.WriteString(http.URLEscape(v))
buf.WriteByte('&')
}
s := buf.String()
return s[0 : len(s)-1]
}
On Aug 6, 8:19 am, enigma wrote:
Hi,
What would be the idiomatic way to create a url query string
(mapToQueryString) from a
map.
m map[stiring] string {
"a": "xZZ",
"b": "sZZ"

}

var url http.URL
url.RawQuery = mapToQueryString(m)
Thanks
--

Search Discussions

  • Michaelcochez at Feb 24, 2014 at 10:41 pm
    Yet another take on the same problem: just let the library do all the work.

    http://play.golang.org/p/sxmhsLQOYw

    baseUrl, err := url.Parse("http://google.com/search")
    if err != nil {
    log.Fatal(err)
    }

    params := url.Values{}
    params.Add("pass%word", "key%20word")

    baseUrl.RawQuery = params.Encode()
    fmt.Println(baseUrl)

    Michael
    On Sunday, January 13, 2013 8:23:10 AM UTC+2, Cody Goodman wrote:

    This came in a search recently for me, and there is since what I believe a
    more idiomatic way of doing this:

    For those who want an example to play with:
    http://play.golang.org/p/kcBAs--SZy

    For those who can't be bothered to click through to another link and would
    otherwise just copy paste the code above:

    package main

    import (
    "fmt"
    "net/url"
    )

    func main() {
    // way 1
    baseUrl := "http://google.com/search?"
    params := url.Values{}
    params.Add("query", "keyword")
    finalUrl := baseUrl + params.Encode()
    fmt.Println(finalUrl)
    }
    On Friday, August 6, 2010 12:39:25 PM UTC-5, Michael Hoisie wrote:

    There isn't a library method to do this, but I've used this code
    before:

    func Urlencode(data map[string]string) string {
    var buf bytes.Buffer
    for k, v := range data {
    buf.WriteString(http.URLEscape(k))
    buf.WriteByte('=')
    buf.WriteString(http.URLEscape(v))
    buf.WriteByte('&')
    }
    s := buf.String()
    return s[0 : len(s)-1]
    }
    On Aug 6, 8:19 am, enigma wrote:
    Hi,
    What would be the idiomatic way to create a url query string
    (mapToQueryString) from a
    map.
    m map[stiring] string {
    "a": "xZZ",
    "b": "sZZ"

    }

    var url http.URL
    url.RawQuery = mapToQueryString(m)
    Thanks
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Puntme at Jan 1, 2015 at 10:47 pm
    This is gold, thanks. I would have never seen that the URL struct had a
    field for the query values :D
    On Monday, February 24, 2014 3:22:54 PM UTC-6, michae...@gmail.com wrote:

    Yet another take on the same problem: just let the library do all the work.

    http://play.golang.org/p/sxmhsLQOYw

    baseUrl, err := url.Parse("http://google.com/search")
    if err != nil {
    log.Fatal(err)
    }

    params := url.Values{}
    params.Add("pass%word", "key%20word")

    baseUrl.RawQuery = params.Encode()
    fmt.Println(baseUrl)

    Michael
    On Sunday, January 13, 2013 8:23:10 AM UTC+2, Cody Goodman wrote:

    This came in a search recently for me, and there is since what I believe
    a more idiomatic way of doing this:

    For those who want an example to play with:
    http://play.golang.org/p/kcBAs--SZy

    For those who can't be bothered to click through to another link and
    would otherwise just copy paste the code above:

    package main

    import (
    "fmt"
    "net/url"
    )

    func main() {
    // way 1
    baseUrl := "http://google.com/search?"
    params := url.Values{}
    params.Add("query", "keyword")
    finalUrl := baseUrl + params.Encode()
    fmt.Println(finalUrl)
    }
    On Friday, August 6, 2010 12:39:25 PM UTC-5, Michael Hoisie wrote:

    There isn't a library method to do this, but I've used this code
    before:

    func Urlencode(data map[string]string) string {
    var buf bytes.Buffer
    for k, v := range data {
    buf.WriteString(http.URLEscape(k))
    buf.WriteByte('=')
    buf.WriteString(http.URLEscape(v))
    buf.WriteByte('&')
    }
    s := buf.String()
    return s[0 : len(s)-1]
    }
    On Aug 6, 8:19 am, enigma wrote:
    Hi,
    What would be the idiomatic way to create a url query string
    (mapToQueryString) from a
    map.
    m map[stiring] string {
    "a": "xZZ",
    "b": "sZZ"

    }

    var url http.URL
    url.RawQuery = mapToQueryString(m)
    Thanks
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJan 13, '13 at 6:23a
activeJan 1, '15 at 10:47p
posts3
users3
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase