将一个切片/数组传递给另一个结构体。

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

passing a slice/array to another struct

问题

我有以下对我有效的代码。

params := &cloudformation.CreateStackInput{
	StackName:   aws.String(d.MachineName),
	TemplateURL: aws.String(d.CloudFormationURL),
	Parameters: []*cloudformation.Parameter{
		{
			ParameterKey:   aws.String("KeyName"),
			ParameterValue: aws.String(d.KeyPairName),
		},
	},
}

我想将参数的创建外部化,所以我创建了以下方法。

func (d *Driver) createParams() []cloudformation.Parameter {
	val := "KeyName=Foo|KeyName2=bar"
	s := strings.Split(val, "|")
	a := []cloudformation.Parameter{}

	for _, element := range s {
		pairs := strings.Split(element, "=")
		key := pairs[0]
		value := pairs[1]
		par := cloudformation.Parameter{
			ParameterKey:   aws.String(key),
			ParameterValue: aws.String(value),
		}
		a = append(a, par)
	}

	return a
}

我的问题是如何将createParams的输出传递给CreateStackInputParameters字段?

params := &cloudformation.CreateStackInput{
	StackName:   aws.String(d.MachineName),
	TemplateURL: aws.String(d.CloudFormationURL),
	Parameters:  d.createParams(),
}

上述代码会产生以下错误:

cannot use d.createParams() (type []cloudformation.Parameter) as type []*cloudformation.Parameter in field value
英文:

I have the following code that works for me.

params := &cloudformation.CreateStackInput{
	StackName:   aws.String(d.MachineName),
	TemplateURL: aws.String(d.CloudFormationURL),
	Parameters: []*cloudformation.Parameter{
		{
			ParameterKey:   aws.String("KeyName"),
			ParameterValue: aws.String(d.KeyPairName),
		},
	},
}

I would like to externalize the creation of the Parameters, so I have created the following method.

func (d *Driver) createParams() []cloudformation.Parameter {

val := "KeyName=Foo|KeyName2=bar"

s := strings.Split(val, "|")

a := []cloudformation.Parameter{}

for _, element := range s {

	pairs := strings.Split(element, "=")

	key := pairs[0]
	value := pairs[1]

	par := cloudformation.Parameter{
		ParameterKey:   aws.String(key),
		ParameterValue: aws.String(value),
	}

	a = append(a, par)

}

return a

}

My issue is how to I pass the output of createParams to the Parameters from CreateStackInput?

params := &cloudformation.CreateStackInput{
	StackName:   aws.String(d.MachineName),
	TemplateURL: aws.String(d.CloudFormationURL),
	Parameters:  d.createParam(),
}

The above yields

cannot use d.createParam() (type []cloudformation.Parameter) as type []*cloudformation.Parameter in field value

答案1

得分: 0

指针类型与它们指向的解引用类型不兼容,错误提示告诉你这一点,因为你试图将[]*cloudformation.Parameter设置为[]cloudformation.Parameter。将createParams的返回类型更改为[]*cloudformation.Parameter,并设置par := &cloudformation.Parameter

func (d *Driver) createParams() []*cloudformation.Parameter {
  val := "KeyName=Foo|KeyName2=bar"
  s := strings.Split(val, "|")
  a := []*cloudformation.Parameter{} // a 应该是指针的切片
  for _, element := range s {
      pairs := strings.Split(element, "=")
      key := pairs[0]
      value := pairs[1]
      par := &cloudformation.Parameter{ // & 将 par 转换为参数的指针
          ParameterKey:   aws.String(key),
          ParameterValue: aws.String(value),
      }
      a = append(a, par)
    }
  return a
}
英文:

Pointer types are not compatible with the dereferenced types they point to, the error is telling you this very thing as you are trying to set a []*cloudformation.Parameter to a []cloudformation.Parameter. Change the return type of createParams to []*cloudformation.Parameter and set par := &cloudformation.Parameter.

func (d *Driver) createParams() []*cloudformation.Parameter {
  val := "KeyName=Foo|KeyName2=bar"
  s := strings.Split(val, "|")
  a := []*cloudformation.Parameter{} //a should be a slice of pointers
  for _, element := range s {
      pairs := strings.Split(element, "=")
      key := pairs[0]
      value := pairs[1]
      par := &cloudformation.Parameter{ //& turns par into a pointer to the parameter
          ParameterKey:   aws.String(key),
          ParameterValue: aws.String(value),
      }
      a = append(a, par)
    }
  return a
  }

huangapple
  • 本文由 发表于 2015年11月19日 04:18:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/33789572.html
匿名

发表评论

匿名网友

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

确定