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

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

passing a slice/array to another struct

问题

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

  1. params := &cloudformation.CreateStackInput{
  2. StackName: aws.String(d.MachineName),
  3. TemplateURL: aws.String(d.CloudFormationURL),
  4. Parameters: []*cloudformation.Parameter{
  5. {
  6. ParameterKey: aws.String("KeyName"),
  7. ParameterValue: aws.String(d.KeyPairName),
  8. },
  9. },
  10. }

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

  1. func (d *Driver) createParams() []cloudformation.Parameter {
  2. val := "KeyName=Foo|KeyName2=bar"
  3. s := strings.Split(val, "|")
  4. a := []cloudformation.Parameter{}
  5. for _, element := range s {
  6. pairs := strings.Split(element, "=")
  7. key := pairs[0]
  8. value := pairs[1]
  9. par := cloudformation.Parameter{
  10. ParameterKey: aws.String(key),
  11. ParameterValue: aws.String(value),
  12. }
  13. a = append(a, par)
  14. }
  15. return a
  16. }

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

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

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

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

I have the following code that works for me.

  1. params := &cloudformation.CreateStackInput{
  2. StackName: aws.String(d.MachineName),
  3. TemplateURL: aws.String(d.CloudFormationURL),
  4. Parameters: []*cloudformation.Parameter{
  5. {
  6. ParameterKey: aws.String("KeyName"),
  7. ParameterValue: aws.String(d.KeyPairName),
  8. },
  9. },
  10. }

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

  1. func (d *Driver) createParams() []cloudformation.Parameter {
  2. val := "KeyName=Foo|KeyName2=bar"
  3. s := strings.Split(val, "|")
  4. a := []cloudformation.Parameter{}
  5. for _, element := range s {
  6. pairs := strings.Split(element, "=")
  7. key := pairs[0]
  8. value := pairs[1]
  9. par := cloudformation.Parameter{
  10. ParameterKey: aws.String(key),
  11. ParameterValue: aws.String(value),
  12. }
  13. a = append(a, par)
  14. }
  15. return a

}

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

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

The above yields

  1. 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

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

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.

  1. func (d *Driver) createParams() []*cloudformation.Parameter {
  2. val := "KeyName=Foo|KeyName2=bar"
  3. s := strings.Split(val, "|")
  4. a := []*cloudformation.Parameter{} //a should be a slice of pointers
  5. for _, element := range s {
  6. pairs := strings.Split(element, "=")
  7. key := pairs[0]
  8. value := pairs[1]
  9. par := &cloudformation.Parameter{ //& turns par into a pointer to the parameter
  10. ParameterKey: aws.String(key),
  11. ParameterValue: aws.String(value),
  12. }
  13. a = append(a, par)
  14. }
  15. return a
  16. }

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:

确定