Golang introduction and setup using docker

Golang introduction and setup using docker

Table of contents

No heading

No headings in the article.

Golang(Go) is a strongly-typed, general-purpose, compiled programming language supported by Google which has built-in concurrency and a robust standard library. Go is an open source language built to be high-performant and efficient.

To download and install Go on your computer, head over to Download and Install Go. Click the button to download Go for your Operating System and follow the installation instruction on the page. After installation, open up a terminal and type go run to test your go installation.

Whereas Go can be installed and run on your local computer, in this tutorial, we'll install and run golang in a docker container. You don't have to follow this route but I'll highly recommend it if you are big on the Dev Ops practice.

By containerizing your applications, you can reap the benefit of Portability: deploy to any other system where Docker is running and be sure that your app will perform exactly as it did when you tested it locally.

To follow through with this method, ensure you have Docker installed and running on your machine. You can read up on that here: Docker installation and configuration.

Depending on the folder structure you want to maintain, I have created a root folder called golang where I'll be creating all my Go applications and learning repos. We will be working in a folder called understanding-go inside the golang directory. Go ahead and create a file called dockerfile with the below code snippet:

FROM golang:1.17 as dev

WORKDIR /app

The dockerfile contains all the commands needed to assemble an image. There is nothing fancy in the file except that we're pulling down golang:1.17, specifying the build stage as dev and setting up our WORKDIR as /dev

The next thing is to change directory to the understanding-go folder cd understanding-go and build the docker image from the dockerfile as follows:

cd understanding-go

docker build --target dev . -t go-image

Similarly, we can run the container in interactive mode as follows:

    docker run -it -v ${PWD}:/dev go-image sh

The above command will open up a terminal window inside the container where we can now start running our go applications.

To confirm that Golang has been installed on the container run go version inside the container

go version

you should see:

go version go1.17.5 linux/arm64

or something similar.

Happy Coding!!