Modules

Go 1.11 introduced preliminary support for modules, enabling users to create applications outside of GOPATH and integrating support for versioning and package distribution. The default AWS Lambda function created by the AWS SAM CLI does not include module support, but it is simple to add.

go mod init

In a terminal window, change directory to the root of your SAM app, e.g., sam-app, and copy and paste the following text:

go mod init github.com/aws-samples/golang-workshop

You do not need to do this inside each separate Lambda function, only once at the top of the SAM application where your template.yaml file is located.

This will create the go.mod file for you with your module name, go version, and required packages.

make deps

Next paste the following into the same terminal window:

make deps

This downloads all of your dependencies and creates the go.sum file as outlined in the Go documentation.

Terminal showing results of 'go mod init' and 'make deps' commands.

go mod init

The Makefile target deps created by SAM CLI works for both GOPATH and Go modules without modification.