Golang development environment in Docker

dimensi0n

Erwan ROUSSEL

Posted on November 15, 2020

Golang development environment in Docker

Why

Installing development software was driving me crazy when I came up with this idea. If there is something I always install on my workstation, this is Docker ! So let's create an image then an environment to code in Go without having to install anything else than docker.

How it works

As it is described here : https://github.com/dimensi0n/goindock you have an image with a bunch of useful things installed in it and you have a dockerfile wich has a service based on this image as well as a mariadb based service and an adminer one.

What's inside

  • Alpine as Operating System
  • Docker in docker
  • Golang (latest version)
  • Fish shell and Oh-My-Fish
  • Git
  • Fully configured Vim for Golang

How to use it

Get the docker-compose.yml :

curl -fLo docker-compose.yml https://raw.githubusercontent.com/dimensi0n/goindock/main/docker-compose.yml
Enter fullscreen mode Exit fullscreen mode

Up all that stuff :

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

Then run the environment :

docker-compose run go
Enter fullscreen mode Exit fullscreen mode

Once you've done that you are inside your new development environment, get or clone your project and you can start working on it :)

Ports

If you want your app to be exposed outside of your container, run it on the port 8080 (this is the one the container exposes), then you can go on https://localhost:8080 on your web browser

Use it with Vim

Vim comes fully configurated for Golang in this image.
You have syntax highlighting, Monokai theme.
You also have auto-completion with <Tab> key.

Run :PlugInstall on the first start to install theme and other things then run :GoInstallBinaries.

  • Compile your package with :GoBuild, install it with :GoInstall or test it with :GoTest. Run a single test with :GoTestFunc).
  • Quickly execute your current file(s) with :GoRun.
  • Debug programs with integrated delve support with :GoDebugStart.
  • Go to symbol/declaration with :GoDef.
  • Look up documentation with :GoDoc or :GoDocBrowser. Easily import packages via :GoImport, remove them via :GoDrop.
  • Precise type-safe renaming of identifiers with :GoRename.
  • See which code is covered by tests with :GoCoverage.
  • Add or remove tags on struct fields with :GoAddTags and :GoRemoveTags.
  • Call golangci-lint with :GoMetaLinter to invoke all possible linters (golint, vet, errcheck, deadcode, etc.) and put the result in the quickfix or location list.
  • Lint your code with :GoLint, run your code through :GoVet to catch static errors, or make sure errors are checked with :GoErrCheck.
  • Advanced source analysis tools utilizing guru, such as :GoImplements, :GoCallees, and :GoReferrers.
  • ... and many more! Please see vim-go documentation for more information.

Use it with Visual Studio Code

Thanks to the Remote Container extension of VS Code you can now use a running container to work on. You just have to install the extension and then with the shortcut search for Remote - Container: Attach to a running container.

Visual studio code demo

Conclusion

I hope this will be useful to you, I will probably create a nodejs one in order to work on my javascript projects. Don't hesiate to say what you think about this in comments and also don't hesitate to contribute to the project if you have an idea. You can create an issue or make a pull request 😉

💖 💪 🙅 🚩
dimensi0n
Erwan ROUSSEL

Posted on November 15, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related