The setup of Go language environment under Ubuntu system

Source: Internet
Author: User

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

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.