GO RESTFUL — #4

Manage Go Dependencies with Go Dep within Docker and VS Code Remote-Containers

Minh-Phuc Tran
Level Up Coding
Published in
3 min readSep 9, 2019

--

Having got a full-time local-quality VSCode-powered containerized development environment, in this article, I’m setting up a Go dependency management mechanism that can be well-integrated with existed technologies, Docker and VS Code Remote-Containers.

Dependency Management

Using open source or 3rd party components can save months or years of development. However, being dependent on external packages, you have to control whether to upgrade some packages to newer version to fix security issues or lock them at certain version to avoid incompatibility. And 3rd party components have their dependencies, too, which really leads you into troubles managing them.

Therefore, modern programming languages usually go with a dependency management tool that helps resolve above issues, e.g. Javascript has npm or yarn. I’m looking for something like that in Go.

go-dep — “official experiment” Go dependency management

Luckily enough, Google is developing and experimenting go-dep as their official dependency management tool. Although it’s experimental, go-dep is very stable.

Briefly, go-dep is implemented in Go, so you can easily install it using the go get command. As a dependency management tool, go-dep helps developers solve typical dependency management use cases:

  • Automatically detect dependency graph and fetch appropriate dependencies.
  • Automatically lock dependencies’ versions to produce reproducible builds.
  • Allow developers to upgrade and downgrade dependencies’ versions and automatically regenerate lock.

How to use go-dep is out of this article’s scope. Please refer to its documentation for further details.

Install go-dep to development containers

Having decided to use go-dep, I’m preinstalling it into development image. Simply enough, I just add go get github.com/golang/dep/cmd/dep to current Dockerfile.

Initialize the project with go-dep

To start using go-dep, I have to pre-populate the project with some required files. Fortunately, go-dep implemented a command that helps do it.

dep init

Running above command in the project’s root directory will create 2 new files Gopkg.toml and Gopkg.lock with some initial content. That’s good enough, I’m committing them into the repository.

Update VS Code .devcontainer.json

Next, I’m updating .devcontainer.json to run dep ensure once development container is launched so that all of the project’s dependencies will be pre-populated properly when I open it for the first time. I simply just add "postCreateCommand": "dep ensure" to .devcontainer.json.

There’re also 2 other important updated fields, workspaceMount and workspaceFolder. As default, VS Code will issue the postCreateCommand in the default workspace directory /root/workspaces/go-restful, which is not a valid location with go-dep, it have to be inside $GOPATH (/go). So I had to change those fields properly, too.

Update .gitignore and VC Code configurations

Adding go-dep introduces some new components that should be ignored from Git or VS Code:

  • Gopkg.lock is an autogenerated lock file, which should never be edited manually. I’m ignoring it from VS Code.
  • vendor directory stores dependencies. I’m ignoring it from both VS Code and .gitignore.
# .vscode/settings.json
{
"files.exclude": {
... others,
"Gopkg.lock": true,
"vendor": true
}

# .gitignore
... others
vendor/

I’m good to go!

Now, everything related to development environment look good enough. See you next time about some initial implementation.

Full source code of this article is published here.

--

--

Software Engineer. Documenting my journey at 𝐩𝐡𝐮𝐜𝐭𝐦𝟗𝟕.𝐜𝐨𝐦