Golang Development Environment Construction

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

0 outline

This article summarizes how to configure Golang environment variables and create a directory structure for project when you install go. The following description applies to the Linix environment.


1 Installation and Environment variables

The go installation methods include: Default installation and custom installation, please see here for the official description: Http://golang.org/doc/install

which

(1) The default installation is relatively simple, but requires root permissions, the default installation path is:/usr/local/go;

(2) Custom installation requires additional configuration of the GOROOT environment variable; listen.


This article uses the default installation. The following environment variables need to be added to the current user's. BASHRC configuration. which

(1) show that the bin installation directory of Go is configured in the PATH environment variable;

(2) Note that Gopath is not the Go installation directory, but the user's working directory (customizable), the Go command relies on Gopath, the important environment variable.

#聽Golang聽enviroment聽variableexport聽GOROOT =/usr/local/goexport Listen path= $PATH: $GOROOT/binexport Listen to gopath=/users/ Gerryyang/code/go/mygo


2 project directory Structure

Description of Gopath:

(a) Gopath allows multiple directories, and when there are multiple directories, be aware of the colon delimiter. When there are multiple Gopath, the contents of go get are placed in the first directory by default.

(b) The Gopath directory has a contract of three subdirectories:

---src store source code (e.g.. Go/. C/. h/. S, etc.)

---The files generated by the PKG after compilation (for example:. a)

---The executable file generated after the bin is compiled (it is not recommended to put this directory under path, although it is convenient!) )


A simple project that calculates the square root of a number, gopath as above, the initial directory structure is as follows:

Note: It is recommended that the package name and directory name be consistent, for example, Main.go in Mathapp packages, and in the Mathapp directory.


Sqrt.go


Package Listen to Mymathfunc sqrt (x listen to float64) listen to float64 listen to {Z Listen: = Listen to 0.0for listen I listen: = listen to 0; listen I listen < listen 1000; listen to the i++ listen to {z Listen to = Listen (Z*z listen-listen x) Listen/Listen (2 Listen * listen x)} Return to listen Z}


Main.go


Package Listen Mainimport ("FMT" "MyMath") Func listen to main () listen {FMT. Printf ("Use listen to my hear package listen to Calc, listen to sqrt (2) =%v\n", listen to MyMath. SQRT (2))}


since we have configured the GOPATH environment variable, we can compile the installation in two ways user-defined packages :


(1) Enter the corresponding application package directory , and then perform a listen to goinstall, you can complete the installation;

(2) Execute code in any directory listen to go install YourApp;


For the example above, compile the installation using the following steps:

(1) Enter mymath/, execute go install MyMath, at this time the directory structure is as follows:


(2) Enter mathapp/, execute go build, at this time the directory structure is as follows:


(3) Enter mathapp/, execute go install, at this time the directory structure is as follows:


(4) in fact (2) and (3) step can be combined into one step, into the mathapp/, directly execute go install, the directory structure is as follows:

(5) Execute the test procedure, enter the bin/and execute the program:


3 references

[1] Listening to go web programming

[2] Listen to Http://golang.org/doc/install


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.