DEV Community

loading...

Getting started to writing an App by Go

Yusuke Hatanaka
Hi.
Originally published at Medium on ・2 min read

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.

Preparing the development environment for Go in late 2017

direnv is a tool that an environment switcher for the shell.

direnv/direnv

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

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

Install dep

dep is a tool that for Go dependency management.

golang/dep

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

standalone apps

The definition of standalone is whether it is referenced by 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
Enter fullscreen mode Exit fullscreen mode

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

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!

Discussion (0)