英文:
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的输出传递给CreateStackInput的Parameters字段?
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
  }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论