我正在尝试将一组键、值传递给 Go 中的另一个函数。对 Go 非常陌生,所以我正在努力弄清楚。
package main
import(
"net/http"
"fmt"
"io/ioutil"
"net/url"
)
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("test-api.dev", &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
}
我收到错误:
val.key undefined (type Params has no field or method key)
val.value undefined (type Params has no field or method key)
然而,当我编译时。
去游乐场链接http://play.golang.org/p/CQw03wZmAV
提前致谢!
肥皂起泡泡
慕侠2389804
相关分类