DEV Community

Cover image for Learning GO - Creating the environment using docker - Part 1
Augusto Queirantes
Augusto Queirantes

Posted on • Updated on

Learning GO - Creating the environment using docker - Part 1

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

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

docker-compose up -d
Enter fullscreen mode Exit fullscreen mode

If everything is ok you should see something like this:

Image description

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

docker exec -it learning_go sh
Enter fullscreen mode Exit fullscreen mode

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!")
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

You should see the following output:

Image description

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 and set the repo name such as the image bellow

Image description

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

Image description

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

Top comments (1)

Collapse
 
andreleoni profile image
André Leoni

👏👏👏👏👏👏