Menu
AWS Cloud9
User Guide

Go Sample for AWS Cloud9

This sample enables you to run some Go code in an AWS Cloud9 development environment.

Note

This sample assumes you already have the AWS Cloud9 IDE for your AWS Cloud9 development environment open in your web browser, and that you're using an AWS Cloud9 EC2 development environment that is connected to an Amazon EC2 instance running Amazon Linux. In you're using a different operating system or using an AWS Cloud9 SSH development environment, you might need to adapt this sample's instructions to correctly install and configure this sample's required tools. To create an environment, see Creating an Environment.

When you're using this sample, be sure you're signed in to AWS with the AWS account ID and name and password of the user you created or identified in Team Setup.

Creating this sample may result in charges to your AWS account. These include possible charges for services such as Amazon EC2 and Amazon S3. For more information, see Amazon EC2 Pricing and Amazon S3 Pricing.

Step 1: Install Required Tools

In this step, you install and configure Go, which is required to run this sample.

  1. In a terminal session in the AWS Cloud9 IDE, confirm whether Go is already installed by running the go version command. (To start a new terminal session, on the menu bar, choose Window, New Terminal.) If successful, the output should contain the Go version number. Otherwise, an error message should be output. If Go is installed, skip ahead to Step 2: Add Code.

  2. Run the yum update command to help ensure the latest security updates and bug fixes are installed.

    sudo yum -y update
  3. To install Go, run these commands, one at a time.

    wget https://storage.googleapis.com/golang/go1.7.5.linux-amd64.tar.gz # Download Go. sudo tar -C /usr/local -xzf ./go1.7.5.linux-amd64.tar.gz # Install Go. rm ./go1.7.5.linux-amd64.tar.gz # Delete the installer.

    For more information, see Downloads on The Go Programming Language website.

  4. Add the path to the Go binary to your PATH environment variable, like this.

    1. Open your shell profile file (for example, ~/.bashrc in Amazon Linux) for editing.

    2. At the end of this line of code, type the following, so that the code now looks like this.

      PATH=$PATH:/usr/local/go/bin
    1. Save the file.

  5. Source the ~/.bashrc file so that the terminal can now find the Go binary you just referenced.

    . ~/.bashrc
  6. Confirm that Go is now successfully installed and configured by running the go version command. If successful, the output contains the Go version number.

Step 2: Add Code

In the AWS Cloud9 IDE, create a file with this content, and save the file with the name hello.go. (To create a file, on the menu bar, choose File, New File. To save the file, choose File, Save.)

package main import ( "fmt" "os" "strconv" ) func main() { fmt.Printf("Hello, World!\n") fmt.Printf("The sum of 2 and 3 is 5.\n") first, _ := strconv.Atoi(os.Args[1]) second, _ := strconv.Atoi(os.Args[2]) sum := first + second fmt.Printf("The sum of %s and %s is %s.", os.Args[1], os.Args[2], strconv.Itoa(sum)) }

Step 3: Run the Code

  1. In the AWS Cloud9 IDE, on the menu bar, choose Run, Run Configurations, New Run Configuration.

  2. On the [New] - Idle tab, choose Runner: Auto, and then choose Go.

    Note

    If Go is not available, you can create a custom runner for Go.

    1. On the [New] - Idle tab, choose Runner: Auto, and then choose New Runner.

    2. On the My Runner.run tab, replace the tab's contents with this code.

      { "cmd" : ["go", "run", "$file", "$args"], "info" : "Running $project_path$file_name...", "selector" : "source.go" }
    3. Choose File, Save As on the menu bar, and save the file as Go.run in the /.c9/runners folder.

    4. On the [New] - Idle tab, choose Runner: Auto, and then choose Go.

    5. Choose the hello.go tab to make it active.

  3. For Command, type hello.go 5 9. In the code, 5 represents os.Args[1], and 9 represents os.Args[2].

    
                  Output of running the Go code in the AWS Cloud9 IDE
  4. Choose the Run button, and compare your output.

    Hello, World! The sum of 2 and 3 is 5. The sum of 5 and 9 is 14.

Step 4: Install and Configure the AWS SDK for Go

You can enhance this sample to use the AWS SDK for Go to create an Amazon S3 bucket, list your available buckets, and then delete the bucket you just created.

In this step, you install and configure the AWS SDK for Go, which provides a convenient way to interact with AWS services such as Amazon S3, from your Go code. Before you install the AWS SDK for Go, you must set your GOPATH environment variable. After you install the AWS SDK for Go and set your GOPATH environment variable, you must set up credentials management in your environment. The AWS SDK for Go needs these credentials to interact with AWS services.

To set your GOPATH environment variable

#. Confirm that the GOPATH environment variable is successfully set by running the echo $GOPATH command. If successful, /home/ec2-user/environment/go should be output.

  1. Open your ~/.bashrc file for editing.

  2. After the last line in the file, type this code.

    GOPATH=~/environment/go export GOPATH
  3. Save the file.

  4. Source the ~/.bashrc file so that the terminal can now find the GOPATH environment variable you just referenced.

    . ~/.bashrc

To install the AWS SDK for Go

Run the go get command, specifying the location of the AWS SDK for Go source.

go get -u github.com/aws/aws-sdk-go/...

Go installs the AWS SDK for Go source into the location specified by your GOPATH environment variable, which is the go folder in your environment.

To set up credentials management in your environment

Each time you use the AWS SDK for Go to call an AWS service, you must provide a set of credentials with the call. These credentials determine whether the AWS SDK for Go has the appropriate permissions to make that call. If the credentials don't cover the appropriate permissions, the call will fail.

In this step, you store your credentials within the environment. To do this, follow the instructions in Call AWS Services from an Environment, and then return to this topic.

For additional information, see the "Specifying Credentials" section of SDK Configuration in the AWS SDK for Go Developer Guide.

Step 5: Add AWS SDK Code

In this step, you add some more code, this time to interact with Amazon S3 to create a bucket, list your available buckets, and then delete the bucket you just created. You will run this code later.

In the AWS Cloud9 IDE, create a file with this content, and save the file with the name s3.go.

package main import ( "fmt" "os" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/s3" ) func main() { sess := session.Must(session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable, })) svc := s3.New(sess, &aws.Config{ Region: aws.String("YOUR_REGION"), }) listMyBuckets(svc) createMyBucket(svc, os.Args[1]) listMyBuckets(svc) deleteMyBucket(svc, os.Args[1]) listMyBuckets(svc) } // List all of your available buckets in this AWS Region. func listMyBuckets(svc *s3.S3) { result, err := svc.ListBuckets(nil) if err != nil { exitErrorf("Unable to list buckets, %v", err) } fmt.Println("My buckets now are:\n") for _, b := range result.Buckets { fmt.Printf(aws.StringValue(b.Name) + "\n") } fmt.Printf("\n") } // Create a bucket in this AWS Region. func createMyBucket(svc *s3.S3, bucketName string) { fmt.Printf("\nCreating a new bucket named '" + bucketName + "'...\n\n") _, err := svc.CreateBucket(&s3.CreateBucketInput{ Bucket: aws.String(bucketName), }) if err != nil { exitErrorf("Unable to create bucket, %v", err) } } // Delete the bucket you just created. func deleteMyBucket(svc *s3.S3, bucketName string) { fmt.Printf("\nDeleting the bucket named '" + bucketName + "'...\n\n") _, err := svc.DeleteBucket(&s3.DeleteBucketInput{ Bucket: aws.String(bucketName), }) if err != nil { exitErrorf("Unable to delete bucket, %v", err) } } // If there's an error, display it. func exitErrorf(msg string, args ...interface{}) { fmt.Fprintf(os.Stderr, msg+"\n", args...) os.Exit(1) }

In the preceding code, replace YOUR_REGION with the ID of an AWS Region. For example, for the US East (Ohio) Region, use us-east-2. For more IDs, see Amazon Simple Storage Service (Amazon S3) in the Amazon Web Services General Reference.

Step 6: Run the AWS SDK Code

  1. In the AWS Cloud9 IDE, on the menu bar, choose Run, Run Configurations, New Run Configuration.

  2. On the [New] - Idle tab, choose Runner: Auto, and then choose Go.

  3. For Command, type s3.go YOUR-BUCKET-NAME , where YOUR-BUCKET-NAME is the name of the bucket you want to create and then delete.

    Note

    Amazon S3 bucket names must be unique across AWS—not just your AWS account.

    
                  Running the AWS SDK for Go code in the AWS Cloud9 IDE
  4. Choose the Run button, and compare your output.

    My buckets now are: Creating a new bucket named 'my-test-bucket'... My buckets now are: my-test-bucket Deleting the bucket named 'my-test-bucket'... My buckets now are:

Step 7: Clean Up

To prevent ongoing charges to your AWS account after you're done using this sample, you should delete the environment. For instructions, see Deleting an Environment.