在Go语言中,是可以将结构体作为参数传递的。

huangapple go评论83阅读模式
英文:

Is it possible to pass a struct as a parameter in Go?

问题

我正在尝试将一个键值对数组传递给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{
		Params{items: []KeyValue{{key: "title", value: "Thingy"}, {key: "body", value: "Testing 123"}}},
	}

	response, err := makePost("test-api.dev", data)
}

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

	v := url.Values{}

	for _, val := range values {
		for _, item := range val.items {
			v.Add(item.key, item.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)

但是当我编译时没有出错。

Go Playground链接:http://play.golang.org/p/CQw03wZmAV

提前感谢!

英文:

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

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
}

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 http://play.golang.org/p/CQw03wZmAV

Thanks in advance!

答案1

得分: 2

values 是一个 []Params。当你对它进行迭代时,val 将是一个 Params,但你却将其当作一个 KeyValue 来处理。你是否真的打算传递一个 []Params,而不仅仅是一个 Params 或者只是一个 []KeyValue

英文:

values is a []Params. When you iterate over it, val will be a Params, but you're treating it as though it was a KeyValue. Did you actually mean to pass an []Params at all, instead of just a Params or even just a []KeyValue?

答案2

得分: 1

如其他人所提到的,你有一个[]Params,但你试图用KeyValue进行初始化。

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

相反,尝试使用以下方式:

data := &Params{items: []KeyValue{
	{key: "title", value: "Thingy"},
	{key: "body", value: "Testing 123"},
}}
英文:

As others have mentioned, you have an []Params but you are trying to initialize with KeyValues.

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

Instead, try:

data := &Params{items: []KeyValue{
	{key: "title", value: "Thingy"},
	{key: "body", value: "Testing 123"},
}}

答案3

得分: 0

感谢大家,最终通过以下方式使其工作:http://play.golang.org/p/aCKU5bTnhT

package main

import (
	"fmt"
)

type Params struct {
	items []KeyValue
}

type KeyValue struct {
	key   string
	value string
}

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

	makePost("test-api.dev", data)
}

func makePost(urlString string, values Params) string {

	for _, val := range values.items {
		fmt.Println("%s", val.key+":"+val.value)
	}

	return string("test")
}
英文:

Thanks folks, finally got it working with: http://play.golang.org/p/aCKU5bTnhT

package main

import (
	"fmt"
)

type Params struct {
	items []KeyValue
}

type KeyValue struct {
	key   string
	value string
}

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

	makePost("test-api.dev", data)
}

func makePost(urlString string, values Params) string {

	for _, val := range values.items {
		fmt.Println("%s", val.key+":"+val.value)
	}

	return string("test")
}

huangapple
  • 本文由 发表于 2015年3月31日 22:37:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/29371278.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定