更好的初始化

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

Better initialization

问题

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

// 构建输入
self := "self"
ownerSelf := []*string{&self}
ownImages := &ec2.DescribeImagesInput{
    Owners: ownerSelf,
}

// 调用DescribeImages操作
resp, err := svc.DescribeImages(ownImages)
if err != nil {
    panic(err)
}

像这样构建输入的方式很丑陋。我相信有更好的技巧,但作为一个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:

// Build input
self := "self"
ownerSelf := []*string{&self}
ownImages := &ec2.DescribeImagesInput{
	Owners: ownerSelf,
}

// Call the DescribeImages Operation
resp, err := svc.DescribeImages(ownImages)
if err != nil {
	panic(err)
}

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

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

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

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

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

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

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

func sp(es ...string) []*string {
    s := make([]*string, len(es))
    for i := range es {
        s[i] = &es[i]
    }
    return s
}

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

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

Go Playground上尝试一下。

英文:

You can't get it any shorter than this:

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

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

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:

func sp(es ...string) []*string {
	s := make([]*string, len(es))
	for i := range es {
		s[i] = &es[i]
	}
	return s
}

With this, the delcaration becomes:

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

Try it on the Go Playground.

答案2

得分: 0

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

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

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

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

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

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

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

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

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:

确定