Go language Gopath and working directory detailed _golang

Source: Internet
Author: User
Tags mkdir using git

Gopath settings

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

(Note: This is not the Go installation directory.) Following the author's working directory for instructions, please replace the working directory on your own machine. )

In similar Unix environments this is probably set:

Copy Code code as follows:

Export Gopath=/home/apple/mygo

For convenience, the new folder should be created and the above line added to the. bashrc or. ZSHRC or the profile of your own sh.

The Windows settings are as follows, and a new environment variable name is called Gopath:

Copy Code code as follows:

Gopath=c:\mygo

Gopath allows multiple directories, when there are multiple directories, please note that separator, multiple directories when Windows is a semicolon, Linux system is a colon, when there are multiple Gopath, the default will be the contents of go to the first directory

The above $GOPATH directory conventions have three subdirectories:

1.SRC Store source code (for example:. Go. C. H. S, etc.)
2.pkg compiled files (such as:. a)
3.bin compiled executable file (for convenience, you can add this directory to $PATH variable, and if there are multiple gopath, then use ${gopath//://bin:}/bin for all bin directories)

In the future, all my examples are based on Mygo as my Gopath catalogue.

Apply directory Structure

Building Packages and Directories: $GOPATH/src/mymath/sqrt.go (Package name: "MyMath")

Create a new application later or a code package is in the SRC directory to create a new folder, the folder name is generally the code package name, of course, also allows multilevel directories, such as under src New directory $gopath/src/github.com/astaxie/beedb So the package path is "Github.com/astaxie/beedb", and the package name is the last directory Beedb

Execute the following code:

Copy Code code as follows:

CD $GOPATH/SRC
mkdir MyMath

Create a new file Sqrt.go, which reads as follows:

Copy Code code 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
}

So my application package directory and code has been created, note: generally recommended package name and directory name consistent

Compiling applications

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

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

After installation, we can enter the following directory:

Copy Code code as follows:

CD $GOPATH/pkg/${goos}_${goarch}
You can see the following file
Mymath.a

This. A file is an application package, so how do we invoke it?

Next we create a new application to invoke the

New Application Package Mathapp:

Copy Code code as follows:

CD $GOPATH/SRC
mkdir Mathapp
CD Mathapp
Vim Main.go

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

Copy Code code 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? Enter the application directory and then execute the go build, and a Mathapp executable file is generated below the directory

Copy Code code as follows:

./mathapp

Output the following content

Copy Code code as follows:

Hello, world. SQRT (2) = 1.414213562373095

How to install the application, enter the directory to perform go install, then add an executable file Mathapp under $gopath/bin/so that you can enter the following command at the command line to perform

Copy Code code as follows:

Mathapp

Also output the following content

Copy Code code as follows:

Hello, world. SQRT (2) = 1.414213562373095

Get Remote Package

The Go language has a tool to get a remote package that is go gets, which currently supports most open source communities (for example: GitHub, Googlecode, BitBucket, Launchpad)

Copy Code code as follows:

Go to Github.com/astaxie/beedb

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

Through the above obtained code in our local source code corresponding to the following structure:

Copy Code code as follows:

$GOPATH
Src
|--github.com
|-astaxie
|-beedb
Pkg
|--The corresponding platform
|-github.com
|--astaxie
|beedb.a

Go-get is essentially understood as the first step is to go through the Source Tool clone code to the SRC below, and then execute went install

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

Copy Code code as follows:

Import "Github.com/astaxie/beedb"

The overall structure of the program

The directory structure of my local Mygo established above is shown below

Copy Code code 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 see very clearly, the bin directory is stored below the compiled executable file, pkg is stored below the function of the package, SRC Save the application source code.

[1] environment variables in Windows systems are in the form of%gopath%, this book is primarily in UNIX form, and Windows users should replace them.

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.