This is a creation in Article, where the information may have evolved or changed.
1. Use the apt-get
command to install the GO environment
Install software-properties-commonaptinstall python-software-propertiesadd- Apt-repository ppa:gophers/goapt-get updateaptinstall golang-go git-core Mercurial
2, set the Gopath variable, specify the working space
The. bashrc file in the current directory finally added export Gopath=/opt/go
Echo " Export Gopath=/opt/go " >> ~/.BASHRC
Reload. bashrc file
SOURCE ~/.BASHRC
3. Working Space directory structure
/opt/Go -src Store source code (. Go. C. H. S, etc. )-pkg-generated files (. A) -bin executable files generated after compilation
4, Development application package (package name to MyMath for example)
Create the MyMath directory under the SRC directory of the workspace, under which the source file Sqrt.go is created, with the following content:
// $GOPATH/src/mymath/sqrt.go Source code is as follows: Package Mymathfunc Sqrt (x float64) float64 { z:0.0for 0 ; i++ { -= (z*z-x)/(2 * x) } return Z}
Note: It is generally recommended that the package name and directory name remain the same
We have built our own application package, how to compile and install it? There are two ways to install
A, just enter the corresponding application package directory, and then execute go install
, you can install the
B. Execute the following code in any directorygo install mymath
After installation, there is an application package in the $gopath/pkg/mymath/platform type/directory MYMATH.A
5. Develop executable package (package name takes Mathapp as an example)
Create the Mathapp directory under the SRC directory of the workspace, under which the source file Main.go is created, with the following content:
$GOPATH/src/mathapp/main.go Source code is as follows:
Package mainImport ( "MyMath" "FMT")Main () {fmt. Printf (%v\ n", MyMath. Sqrt (2))}
Can see this main
package is, import inside the packet is called mymath
, this is relative to $GOPATH/src
the path, if it is a multi-level directory, import into the multi-level directory.
Go to the app directory and then executego build,那么在该目录下面会生成一个mathapp的可执行文件。运行执行程序如下:
#./Mathapphello, world. Sqrt (21.414213562373095
6. Release and execution procedures
In the application directory, execute go install
, then add an executable file Mathapp under $gopath/bin/, run as follows:
#mathappHello, world. Sqrt (21.414213562373095