-1

I am fairly new to GO and I was hoping someone would be able to point me in the right direction. I am trying to store a Print to a variable. To give some context, currently, I am retrieving the URL of a file that is in Amazon S3 and I would like to store the URL as a variable so I can send it out as say, an email or push notification.

Here is what I have at the moment:

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)
}

This results in extra expression in var declaration when attempting to compile (TBF I did not expect it to work). Would anyone know an alternative way of doing this?

Thanks!

CJW
  • 710
  • 9
  • 26

1 Answers1

5

To store the result of the concatenation, you can use fmt.Sprintf

variable := fmt.Sprintf("https://s3.%s.amazonaws.com/%s/%s", region, bucket, *item.Key)
edkeveked
  • 17,989
  • 10
  • 55
  • 93