Learning GO - Creating the environment using docker - Part 1
Published: 2022-10-12 08:24:53

Hey guys! How are you? Hope everyone is ok!

I'm a Rails developer that wants to learn Go and document the process on this series of articles. It's worth mentioning that I'm learning this technology, so, suggestions are welcome :)

In this article I'll setup docker and test it with a hello world!

# Table of Content

- [Setting up Docker](#setting-up-docker)
- [Creating hello world](#creating-hello-world)
- [Creating Github repository](#creating-github-repository)

# Setting up Docker

The first thing we need to do is create a folder to the repo, you can use the command bellow to create the folder and go to that directory:

```
mkdir learning_go
cd learning_go
```

This command will create a new folder called `learning_go`. All changes we gonna do will be in this folder.

Once the folder is created we need to create two files: `Dockerfile` and `docker-compose.yml`. You can use the following command to create this file:

```
touch Dockerfile
touch docker-compose.yml
```

Once the file is created you'll need to open the folder inside your favorite text editor.

The Dockerfile content will be the following:

```
FROM golang:1.19.2-alpine3.16
```

That's the only line you'll gonna need on this file. It tells docker to download a image that is built based on alpine linux and runs go version 1.19.2.

Now we need to edit `docker-compose.yml`, that's the file content:

```
version: '3.9'

services:
  learning_go:
    container_name: learning_go
    build: .
    volumes:
      - ./:/go/src/app
    ports:
      - "3000:3000"
    working_dir: /go/src/app
    tty: true
```

Once everything is done you need to run the following command:

```
docker-compose up -d
```

If everything is ok you should see something like this:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ov1hs8l5oxa49nxdo7go.png)

If you want to execute commands inside the docker container you must run the following command:

```
docker exec -it learning_go sh
```

# Creating hello world

Now that docker is running as expected we need to create a go file to test it, so we gonna create a file called `main.go` in the root path of the application and set this content:

```
package main

import "fmt"

func main() {
    fmt.Println("hello world!")
}
```

This code is only a basic hello world example. To execute this code you need to run the following command inside the docker container:

```
go run main.go
```

You should see the following output:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lpiuisjpp638r0th3o8n.png)


# Creating Github repository

Now that everything is working we need to store the code in a github repository. To do so you just need to access [this url](https://github.com/new) and set the repo name such as the image bellow


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qenybovzkfc50v0luveq.png)

Once it's done you just need to follow the instruction that github provides:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1wsjlr5vczpdvw3mvm5e.png)


# Conclusion

Well guys, that's it! Thanks for your attention! Let me know if there is any questions that I can help. You can find this project repository [here](https://github.com/augusto-queirantes/learning_go)