2015-03-31 14:37
浏览 28


I'm trying to pass an array of key, values to another function in Go. Very new to Go so am struggling to figure it out.

package main


type Params struct {
    items []KeyValue

type KeyValue struct {
    key string
    value string

func main() {
     data := []Params{
          KeyValue{ key: "title", value: "Thingy" },
          KeyValue{ key: "body", value: "Testing 123" }}

     response, error := makePost("", &data)

func makePost(urlString string, values []Params) (string, error) {

     v := url.Values{}

    for _, val := range values {
        v.Add(val.key, val.value)

    response, err := http.PostForm(urlString, v)

    defer response.Body.Close()

    contents, err :=  ioutil.ReadAll(response.Body)

    if err != nil {
        fmt.Printf("%s", err)

    return string(contents), err

I get errors:

val.key undefined (type Params has no field or method key)
val.value undefined (type Params has no field or method key)

When I compile however.

Go playground link

Thanks in advance!

3条回答 默认 最新

相关推荐 更多相似问题