Golang商店打印为变量

时间:2018-09-24 16:07:59

标签: go

我对GO刚起步,我希望有人能够指出正确的方向。 我正在尝试将打印存储到变量。为了提供一些背景信息,目前,我正在检索Amazon S3中文件的URL,我想将该URL存储为变量,以便可以将其发送为电子邮件或推送通知。

这是我目前的状态:

func main() {
    bucket := "bucket"
    region := "eu-west-2"

    // Initialize a session in us-west-2 that the SDK will use to load
    // credentials from the shared credentials file ~/.aws/credentials.
    sess, err := session.NewSession(&aws.Config{
        Region: aws.String("eu-west-2")},
    )

    // Create S3 service client
    svc := s3.New(sess)


    // Get the list of item
    resp, err := svc.ListObjects(&s3.ListObjectsInput{Bucket: aws.String(bucket)})
    if err != nil {
        exitErrorf("Unable to list items in bucket %q, %v", bucket, err)
    }

    for _, item := range resp.Contents {
        fmt.Printf("https://s3.%s.amazonaws.com/%s/%s", region, bucket, *item.Key)
        fmt.Println("")
        var url string = "https://s3.%s.amazonaws.com/%s/%s", region, bucket, *item.Key
}
}

func exitErrorf(msg string, args ...interface{}) {
    fmt.Fprintf(os.Stderr, msg+"\n", args...)
    os.Exit(1)
}

尝试进行编译时会导致extra expression in var declaration(TBF,我没想到它会起作用)。有人知道这样做的另一种方法吗?

谢谢!

1 个答案:

答案 0 :(得分:2)

要存储连接的结果,可以使用fmt.Sprintf

variable := fmt.Sprintf("https://s3.%s.amazonaws.com/%s/%s", region, bucket, *item.Key)