Gopath and working directory of Go language

Source: Internet
Author: User
Tags using git

This article mainly introduces the Gopath and working directory of Go language, this article explained in detail the Gopath settings, application directory structure, compile applications and other content, the need for friends can refer to the following

Gopath settings

The GO command relies on an important environment variable: $GOPATH 1

(Note: This is not the Go install directory.) Following the author's working directory as a description, please replace the working directory on your own machine. )

In a Unix-like environment this is probably set up:

Copy CodeThe code is as follows:
Export Gopath=/home/apple/mygo

For convenience, create a new folder and add the above line to the. bashrc or. ZSHRC or your own SH configuration file.

Windows is set up as follows, creating a new environment variable name called Gopath:

Copy CodeThe code is as follows:
Gopath=c:\mygo

Gopath allow multiple directories, when there are multiple directories, note the delimiter, multiple directories when Windows is a semicolon, Linux system is a colon, when there are multiple Gopath, the default is to put the contents of go get in the first directory

The above $GOPATH directory Convention has three subdirectories:

1.SRC store source code (e.g.,. Go. C. h, etc.)
2.PKG files generated after compilation (e.g.. a)
3.bin compiled executable file (for convenience, this directory can be added to the $PATH variable, if there are multiple gopath, then use ${gopath//://bin:}/bin to add all Bin directory)

All my examples in the future are Mygo as my Gopath directory.

Application directory Structure

Create packages and directories: $GOPATH/src/mymath/sqrt.go (Package name: "MyMath")

After you create a new application or a code package is in the SRC directory to create a new folder, the folder name is usually the name of the code package, of course, also allow multi-level directory, for example, under the SRC new directory $gopath/src/github.com/astaxie/beedb Then the package path is "Github.com/astaxie/beedb", the package name is the last directory Beedb

Execute the following code:

Copy CodeThe code is as follows:
CD $GOPATH/SRC
mkdir MyMath

Create a new file Sqrt.go with the following content:

Copy CodeThe code is as follows:
$GOPATH/src/mymath/sqrt.go Source code is as follows:
Package MyMath

Func Sqrt (x float64) float64 {
Z: = 0.0
For I: = 0; i < 1000; i++ {
Z-= (z*z-x)/(2 * x)
}
Return Z
}

This way, my application package directory and code have been built, note: The general recommendation is to keep the name and directory name consistent

Compiling the App

We have built our own application package, how to compile and install it? There are two ways to install

1, just go to the corresponding application package directory, and then perform go install, you can install the
2. Execute the following code in any directory go install MyMath

After installation, we can enter the following directory:

Copy CodeThe code is as follows:
CD $GOPATH/pkg/${goos}_${goarch}
You can see the following files
Mymath.a

This. A file is an application package, so how do we make calls?

Next we create a new application to invoke the

New app Package Mathapp:

Copy CodeThe code is as follows:
CD $GOPATH/SRC
mkdir Mathapp
CD Mathapp
Vim Main.go

$GOPATH/src/mathapp/main.go Source:

Copy CodeThe code is as follows:
Package Main

Import (
"MyMath"
"FMT"
)

Func Main () {
Fmt.  Printf ("Hello, World.") SQRT (2) =%v\n ", MyMath. SQRT (2))
}

How do I compile a program? Go to the app directory and execute go build, then a mathapp executable will be generated under that directory

Copy CodeThe code is as follows:
./mathapp

Output the following:

Copy CodeThe code is as follows:
Hello, world. SQRT (2) = 1.414213562373095

How to install the app, go to the directory to go install, then add an executable file Mathapp under $gopath/bin/, so you can enter the command line as below to execute

Copy CodeThe code is as follows:
Mathapp

It also outputs the following content

Copy CodeThe code is as follows:
Hello, world. SQRT (2) = 1.414213562373095

Get Remote Package

Go language has a tool to get the remote package is go get, now go get support most open source community (for example: GitHub, Googlecode, BitBucket, Launchpad)

Copy CodeThe code is as follows:
Go get github.com/astaxie/beedb

The go get-u parameter automatically updates the package and automatically gets the other third-party packages that the package relies on when go get
Through this command can obtain the corresponding source code, the corresponding open source platform using different source control tools, such as GitHub using Git, googlecode using HG, so to obtain these sources, you must first install the corresponding source control tools

The code obtained through the above codes in our local source code is structured as follows:

Copy CodeThe code is as follows:
$GOPATH
Src
|--github.com
|-astaxie
|-beedb
Pkg
|--corresponding Platform
|-github.com
|--astaxie
|beedb.a

Go get is essentially understood as the first step is to go through the Source Code tool, clone, below SRC, and then run

How to use a remote package in your code is as simple as using a local package, as long as you import the appropriate path at the beginning

Copy CodeThe code is as follows:
Import "Github.com/astaxie/beedb"

The overall structure of the program

The directory structure of my local Mygo, as set out above, is as follows

Copy CodeThe code is as follows:
bin/
Mathapp
pkg/
Platform name/such as: Darwin_amd64, LINUX_AMD64
Mymath.a
github.com/
astaxie/
Beedb.a
src/
Mathapp
Main.go
mymath/
Sqrt.go
github.com/
astaxie/
beedb/
Beedb.go
Util.go
From the above structure we can clearly see that the bin directory is stored under the compiled executable file, pkg is stored under the function package, SRC is saved under the application source code.

[1] The form of environment variables in Windows system is%gopath%, this book is mainly used in UNIX form, the Windows users should replace themselves.

Gopath and working directory of Go language

Related Article

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.