Liteide Setting the Gopath

Source: Internet
Author: User
Tags install go
This is a creation in Article, where the information may have evolved or changed.

Mac install Go development environment can draw on my blog before:
The setup of Go language environment under Mac
Http://www.cnblogs.com/ghj1976/archive/2013/01/16/2863142.html

First, you need to set Goroot now

Goroot is the root directory of the go installation package. It is usually placed in a $home/go, and of course it can be another location. For example, my Mac here is installed under/usr/local/go by default.

Reference: Https://code.google.com/p/golang-china/wiki/Install

Settings for Gopath:

Note: This is not the Go install directory.

Reference: HTTPS://GITHUB.COM/ASTAXIE/BUILD-WEB-APPLICATION-WITH-GOLANG/BLOB/MASTER/01.2.MD

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.

There are three sub-directories in the $GOPATH directory:

    • SRC store source code (e.g.,. Go. C. h, etc.)
    • The files generated by the PKG after compilation (for example:. a)
    • Executable file generated after bin compilation (for convenience, this directory can be added to the $PATH variable)

Suppose, as set,/user/cybercare/go is my $GOPATH directory.

The command line executes the following code

CD $GOPATH/SRC
mkdir MyMath

Create a new file Sqrt.go with the following content

$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

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

CD $GOPATH/SRC
mkdir Mathapp
CD Mathapp
Vim Main.go
$GOPATH/src/mathapp/main.go Source:

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

./mathapp
Output the following:

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

Mathapp
It also outputs the following content

Hello, world. SQRT (2) = 1.414213562373095

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.