Eresh Gorantla

13 February 2020

In this article, we’ll discuss the basic usage of the GO module, how to create our own GO module, and how to include secure GO modules in our own GO projects. Our main focus is on private Git repositories.

Modules greatly improve the code maintainability of Go. It is dependency management in the GO language, just like Maven in Java applications. If you are not familiar with the modules in GO, click here to obtain the official documentation.

Create your own GO module

In Java, artifacts are maintained in a repository such as Nexus. Similarly, you can host your own Go module on Git or Bitbucket. We’re hosting it on Github.

All environment-related things must be configured for GoLang. Otherwise, it will not work as expected.

Switch to a directory and create a folder common-module

mkdir common-module
cd common-module
Copy the code

Initialize the Go module in the project with a name. The name format is

<hosting-site>/<repository>/<module>
Copy the code
  • Hosted site :- github.com
  • – Myrepo (Ereshzealous)
  • Module: – common – the module

Go for github.com/ereshzealous/common-module module name

go mod init github.com/ereshzealous/common-module
Copy the code

Let’s create a Commons package and write a simple GO file that contains some sample functions.

//commons.go

package common

// GetCommonData a method
func GetCommonData(a) string {
	return "Common Data"
}

func GetData(a) string {
	return "Data"
}
Copy the code

Now go ahead and publish the Go module to a version on GitHub. Click on the one highlighted below and this will free your GIT commit to public. To learn how to publish to Github, click here.

If you import this module in your GO file, these functions are visible because the repository has no limitations. Now move the repository from public to private.

The development environment

Let’s use the go get command to get the public module. You will see the 410 Gone error message.

To solve the above problem and include private modules in the GO application, there are two steps.

  1. Proxy site through Go Mod

This is because just like Maven Defaultrepo Go, the Go module proxy site also has a proxy site (proxy.golang.org,direct).

But we released it in Github, so we had to provide a mechanism to bypass the proxy site for the private repository. This can be done by using GOPRIVATE.

The new GOPRIVATE environment variable represents a private module path. It serves as the default value for the lower-level GONOPROXY and GONOSUMDB variables, which provide more fine-grained control over which modules are acquired through the proxy and which are validated using the checksum database.

Set up GOPRIVATE in the development environment, as shown below. Multiple values are separated by commas. I have set for my account level below, we also can be set to level of the repository, such as github.com/ereshzealous/common-module

go env -w GOPRIVATE=github.com/ereshzealous
Copy the code
  1. The repository credentials are passed to the Go module during build

When we use Github, we have to provide the Auth token during build time. In a development environment, this is straightforward. We have a git command to do this, which is to add an entry to the gitConfig file. A formatted URL must be passed in order to evaluate credentials during generation.

Note: The Auth token must be URL-encoded.

Click here to go to GitHub access Token. Create one with the appropriate permissions. Create a new token or use an existing token.

After the token is generated, run the following command.

Github

//git.txt

git config --global url."https://${username}:${access_token}@github.com".insteadOf /
"https://github.com"
Copy the code

Bitbucket

In Bitbucket, create an access token and execute the following command.

//bitbucketconfig

git config --global url."https://${bitbucket_user_id}:${bitbucket_access_token}@privatebitbucket.com".insteadOf 
  "https://privateaccount.com"
Copy the code

With that set up in gitConfig, let’s try again to get the GO module.

This applies to development environments, CI/CD or Docker containerization.

Docker

Fortunately, we can do this in Docker files. Here’s how we do it.

//Dockerfile

# Start from the latest golang base image
FROM golang:alpine

RUN GOCACHE=OFF

RUN go env -w GOPRIVATE=github.com/ereshzealous


# Set the Current Working Directory inside the container
WORKDIR /app

# Copy everything from the current directory to the Working Directory inside the container
COPY . .

RUN apk add git

RUN git config --global url."https://golang:<access-token>@github.com".insteadOf "https://github.com"

# Build the Go app
RUN go build -o main .

# Expose port 8080 to the outside world
EXPOSE 8080

#ENTRYPOINT ["/app"]

# Command to run the executable
CMD ["./main"]
Copy the code

Line 6

Set GOPRIVATE as the repository.

Line 17

Gitconfig sets an access token and user name. This will be credentialed to go Mod. To do this, we have git Image in Docker.

Let’s take a look at the docker execution console.

The Pvt is a private repository.

Original link: medium.com/swlh/go-mod…