Go workspace and GOPATH Environment Variables

Source: Internet
Author: User

Go workspace and GOPATH Environment Variables

The Go language does not force certain workspaces and project structures to be used. Small Go programs can be compiled and run directly by running Go run and other commands. However, maintaining a good workspace and file structure is very helpful for managing source code and publishing programs. For large Go language projects, the workspace is required.

1. Go language workspace Structure

The Go language workspace is actually a file directory, which must contain src, pkg, and bin.

The src directory is used to store Go source code, the pkg directory is used to store package objects, and the bin directory is used to store executable objects. You can use the Go compilation command tool to store the source code or package compiled binary output to the bin and pkg directories. The source code in the src directory is classified into the corresponding subdirectories Based on the package name, and various version control tools can be used. For example, the directory structure of the Go workspace is roughly as follows:

Bin/
Hello # executable command
Outyet # executable command
Pkg/
Linux_amd64/
Github.com/golang/example/
Stringutil. a # package object
Src/
Github.com/golang/example/
. Git/# Git repository data
Hello/
Hello. go # source code
Outyet/
Main. go # source code
Main_test.go # test source code
Stringutil/
Reverse. go # package source code
Reverse_test.go # test source code

The workspace above contains a repository named example, which contains the hello and outyet commands and a stringutil library. In addition, a workspace usually contains multiple warehouses.

2. GOPATH Environment Variables

GOPATH is an environment variable related to the workspace in the go language. This variable specifies the workspace location of the go language.

After creating a workspace directory, you need to add the path of the workspace directory to the GOPATH environment variable. The GOPATH environment variable supports multiple values. If you have multiple workspaces, you can add multiple workspaces to this environment variable. The window system uses the Semicolon "; "Separate different values. Linux or Unix systems use the colon": "to separate different values. In addition, add the bin paths of all workspaces to the path environment variable. In Linux, you can ~ /Add the following content at the end of the profile file:

$ Export GOPATH = your workspace path
$ Export PATH = $ PATH: $ GOPATH/bin

Of course, if you have more than one workspace, you cannot directly use $ GOPATH/bin in the PATH variable. You must add the bin paths in each workspace separately.

Note that the GOPATH environment variable value cannot be the same as the installed go directory. The go directory also contains directory structures similar to the workspace, such as src, pkg, and bin. However, the directory structure contains the standard modules of go. It is recommended that you do not mix your workspace with the go directory, it is easier to upgrade the go version later.

Getting started with Linux-installing Go in Linux

Install Go Language Pack in Ubuntu

Go language programming (HD) full-version ebook

Go language and beauty-surpassing "Hello World"

Why do I like the Go language?

Implementation of the Go language memory distributor

This article permanently updates the link address:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.