Getting started to writing an App by Go

Yusuke Hatanaka
2 min readNov 16, 2017

--

We have things to care about when before writing the Go code.
That is about management of GOPATH and vendor package versioning.

GOPATH is having a large package dependency scope by default.
This cause a performance degradation of a build, editor, and other relational tools.
Furthermore, it is difficult to manage package versions. go get fetch packages to the same context of each package.

But we can solve this by changing GOPATH depending on that context.
There are a little bit different method of the standalone apps and reusable packages.

I introduce how to get started.

Install direnv

Before reading this, there is an article I wrote before, I recommend you read that first.

direnv is tool that an environment switcher for the shell.

With this, you can use arbitrary directories as GOPATH.
You can get a minimum scoped development environment in this way.

% echo $GOPATH
/Users/hatajoe
% cd work
% mkdir app
% cd app
% direnv edit .
use go go1.9.2
layout go

By doing this, GOPATH is changed to /Users/hatajoe/work/app when moving into /app directory.

Install dep

dep is tool that for Go dependency management.

By using dep, we can fetch the same revision packages at any time.

standalone apps

The definition of standalone is whether it is referenced from others or not.
If you make a standalone app, doing like this.

% pwd
/Users/hatajoe/work
% mkdir app
% cd app
% direnv edit .
use go go1.9.2
layout go
% git init
% git add .
% git commit -m “initial commit”
% mkdir -p src/app
% cd src/app
% dep init
% ls
Gopkg.lock Gopkg.toml vendor

That’s it! We ready to start coding.

reusable packages

If you make a reusable package, doing like this.

% pwd
/Users/hatajoe/work
% mkdir my-package
% cd my-package
% direnv edit .
use go go1.9.2
layout go
% mkdir -p src/github.com/hatajoe/my-package
% cd src/github.com/hatajoe/my-package
% dep init
% ls
Gopkg.lock Gopkg.toml vendor
% git init
% git add .
% git commit -m “initial commit”

You know that it has a little bit different from standalone apps.

Thank you for reading. Feedback is welcome.
If you have any ideas and improvements feel free to respond me!

--

--