1. Installing Golang
sudo apt-get updatesudo apt-get install Golang
2. Environment configuration
Vi/etc/profile
Insert the following code:
Export goroot=/usr/lib/goexport goarch=386export GOOS=linuxexport gopath=/opt/ gopathexport GOBIN=$GOPATH/binexport PATH=$GOPATH/bin:$PATH
Reload. bashrc file
SOURCE ~/.BASHRC
3. Working Space directory structure
/opt/go -src Store source code (. Go. C. H. s etc.) -pkg compiled post-build file (. A) -bin compiled executable file
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 Sqrt(x float64) float64 { 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 main import ( " 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的可执行文件。运行执行程序如下:
# ./mathapp Hello, 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 (2) = 1.414213562373095
The setup of Go language environment under Ubuntu system