更好的初始化

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

Better initialization

问题

我正在使用Golang SDK向AWS发出API调用,以获取AMI列表。DescribeImages函数接受DescribeImagesInput作为输入。我只想看到自己的AMI,所以我的代码是这样写的:

  1. // 构建输入
  2. self := "self"
  3. ownerSelf := []*string{&self}
  4. ownImages := &ec2.DescribeImagesInput{
  5. Owners: ownerSelf,
  6. }
  7. // 调用DescribeImages操作
  8. resp, err := svc.DescribeImages(ownImages)
  9. if err != nil {
  10. panic(err)
  11. }

像这样构建输入的方式很丑陋。我相信有更好的技巧,但作为一个Golang新手,我不知道。有没有更好的方法?

英文:

I am making an API call to AWS to get a list of AMIs using the Golang SDK. The DescribeImages function takes in DescribeImagesInput. I only want to see my own AMIs, so my code is doing this:

  1. // Build input
  2. self := "self"
  3. ownerSelf := []*string{&self}
  4. ownImages := &ec2.DescribeImagesInput{
  5. Owners: ownerSelf,
  6. }
  7. // Call the DescribeImages Operation
  8. resp, err := svc.DescribeImages(ownImages)
  9. if err != nil {
  10. panic(err)
  11. }

Building input like that is very ugly. I am sure there is a better technique, but being a Golang n00b, I don't know it. What is a better way to do?

答案1

得分: 2

你可以将代码翻译为以下形式:

  1. self := "self"
  2. ownImages := &ec2.DescribeImagesInput{Owners: []*string{&self}}

或者一行代码实现(不使用self字符串变量):

  1. ownImages := &ec2.DescribeImagesInput{Owners: []*string{&[]string{"self"}[0]}}

这里的操作是创建一个[]string切片,其中包含一个元素"self",然后索引其唯一的元素,取其地址并使用该值初始化所需的[]*string

你可以创建一个辅助函数来为你构建string指针的切片:

  1. func sp(es ...string) []*string {
  2. s := make([]*string, len(es))
  3. for i := range es {
  4. s[i] = &es[i]
  5. }
  6. return s
  7. }

使用这个函数,声明可以变为:

  1. ownImages = &ec2.DescribeImagesInput{Owners: sp("self")}

Go Playground上尝试一下。

英文:

You can't get it any shorter than this:

  1. self := "self"
  2. ownImages := &ec2.DescribeImagesInput{Owners: []*string{&self}}

Or in one line (without the self string variable):

  1. ownImages := &ec2.DescribeImagesInput{Owners: []*string{&[]string{"self"}[0]}}

(What happens here is I create a []string slice with one element "self", index its only element and take its address and use this value to initialize the required []*string.)

What you can do is create a helper function which constructs the slice of string pointers for you:

  1. func sp(es ...string) []*string {
  2. s := make([]*string, len(es))
  3. for i := range es {
  4. s[i] = &es[i]
  5. }
  6. return s
  7. }

With this, the delcaration becomes:

  1. ownImages = &ec2.DescribeImagesInput{Owners: sp("self")}

Try it on the Go Playground.

答案2

得分: 0

我不知道这个更可接受程度有多高,但它应该做同样的事情:

  1. self := "self"
  2. resp, err := svc.DescribeImages(
  3. &ec2.DescribeImagesInput{
  4. Owners: []*string{&self},
  5. },
  6. }
  7. if err != nil {
  8. panic(err)
  9. }

当然,这可以缩短,但可能会降低可读性:

  1. self := "self"
  2. resp, err := svc.DescribeImages(&ec2.DescribeImagesInput{Owners:[]*string{&self}})
  3. if err != nil {
  4. panic(err)
  5. }
英文:

I don't know how much more acceptable this is, but it should do the same:

  1. self := "self"
  2. resp, err := svc.DescribeImages(
  3. &ec2.DescribeImagesInput{
  4. Owners: []*string{&self},
  5. },
  6. }
  7. if err != nil {
  8. panic(err)
  9. }

Which could of course be shortened, albeit at the cost of readability IMO

  1. self := "self"
  2. resp, err := svc.DescribeImages(&ec2.DescribeImagesInput{Owners:[]*string{&self}}}
  3. if err != nil {
  4. panic(err)
  5. }

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

发表评论

匿名网友

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

确定