This page looks best with JavaScript enabled

Go Development Configuration

 ·  ☕ 4 min read

1. Common Environment Variables

1.1 GOROOT

$GOROOT is the local directory where the Go package is installed.

1
2
3
4
cd /c/Go
ls
AUTHORS          CONTRIBUTORS  PATENTS    VERSION  bin  favicon.ico  misc  robots.txt  test
CONTRIBUTING.md  LICENSE       README.md  api      doc  lib          pkg   src

1.2 GOPATH

$GOPATH is the Go workspace directory, used to hold code, third-party libraries, compiled intermediate files, and so on.

If you need to configure multiple directories, on Mac and Linux you can separate them with :, and on Windows you can use ;.

Note that packages fetched by go get are stored in the first directory by default.

Under the $GOPATH directory, there are conventionally three subdirectories:

  • src: holds source code
  • pkg: holds the intermediate files generated during compilation
  • bin: holds the executable files produced by compilation. Usually you add ;%GOPATH%\bin to the PATH environment variable so that the compiled programs are easy to run.

In project development, there are two kinds of code to manage:

  • Third-party libraries
  • Business project source code

There are three ways to manage GOPATH:

  • Multiple projects sharing a single global GOPATH. Simple, but with insufficient project isolation.
  • Third-party libraries and business source code placed separately, configuring two GOPATHs.
  • Each project using its own separate GOPATH, with a script to compile it.

Usually, for simplicity, multiple projects share one GOPATH.

1
2
3
cd /c/data/go/src
ls
project1  project2  github.com  gitlab.com

/c/data/go/ is the GOPATH directory I configured.

In the src directory there are both third-party libraries such as github\gitlab and business-related project code.

1.3 GOBIN

GOBIN is the installation directory for executables after compilation. If GOBIN is set, the compiled executables will not be installed into the bin directory under GOPATH. If GOPATH contains multiple directories, then GOBIN must be set.

2. Dependency Management

Under the GOPATH path there is not only project code but also a large number of third-party dependency libraries. When there are many projects and dependencies, the amount of code under the GOPATH path becomes very large and hard to manage.

2.1 vendor

Starting with 1.5, Go added the vendor mechanism. The vendor mechanism is simply adding a vendor folder to a project to hold dependencies and isolate the project.

When using go run or go build, Go first looks for dependencies in the vendor folder under the current path, and if not found there, queries GOPATH. But when using the go get or go install commands, Go still installs dependencies into GOPATH.

2.2 dep

Installation command:

1
go get -u github.com/golang/dep/cmd/dep

Basic commands:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
dep
Dep is a tool for managing dependencies for Go projects

Usage: "dep [command]"

Commands:

  init     Set up a new Go project, or migrate an existing one
  status   Report the status of the project's dependencies
  ensure   Ensure a dependency is safely vendored in the project
  version  Show the dep version information
  check    Check if imports, Gopkg.toml, and Gopkg.lock are in sync

Examples:
  dep init                               set up a new project
  dep ensure                             install the project's dependencies
  dep ensure -update                     update the locked versions of all dependencies
  dep ensure -add github.com/pkg/errors  add a dependency to the project

The help documentation already describes how to use dep quite clearly. In addition, godep and go modules can also be used to manage dependency packages.

3. Basic Commands

  • go env

View all of go’s configuration information.

1
2
3
4
go env
set GOARCH=amd64
set GOBIN=
set GOHOSTARCH=amd64
  • go get

Download or update the specified dependency package, then compile and install it.

1
go get github.com/astaxie/beego
  • go build

Compile source files and dependency packages.

  • go install

Compile and install the specified dependency package.

  • go clean

Remove files and directories produced by other commands.

  • go run

Compile and run a source file.

4. Hello World

Download and install

1
go get github.com/astaxie/beego

Create the file hello.go

1
2
3
4
5
6
7
package main

import "github.com/astaxie/beego"

func main() {
    beego.Run()
}

Compile and run

1
2
3
go build hello.go
./hello.exe
2019/01/23 21:40:59.535 [I]  http server Running on http://:8080

Open a browser and visit http://localhost:8080


微信公众号
WRITTEN BY
微信公众号