Golang engineering Conventions, subcontracting and compilation

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

Go-lang has released the go1.1.2, some time ago to see the go language tutorial, like the language, but the use of multiple files compiled is encountered some trouble, reference error compiled pass, Google found the relevant tutorial:

Https://golang.org/doc/code.html

http://lavafree.iteye.com/blog/1683363,

Directory structure of the HTTP://BLOG.STUDYGOLANG.COM/2012/12/GO project

And according to their own understanding of the record, and carried out the experiment. My project directory is as follows:

Goproject                          # project root directory |.........src                      # source code            |.....com               | Main                    |...main.go    #这个必须在main包, there is a main function               |......sqrt                    |...sqrt.go    #包内源代码文件, does not contain the main function

As the above structure is the directory structure of Go, in fact, bin and Pkg is not built by themselves, go compile automatically generated.


Actual combat:

1. Create a new Goproject directory under D:\

2. Setting Gopath Environment variables

Gopath = D:\goprojcet

New SRC under 3.goproject

4.SRC new sqrt, new sqrt and sqrt.go files

5.sqrt.go File Input Code

Package sqrt//sqrt returns-approximation to the square root of X.func sqrt (x-float64) float64 {//This is a terrible I mplementation.//Real code should import "math" and use math. Sqrt.z: = 0.0for I: = 0; i < 1000; i++ {z-= (z*z-x)/(2 * x)}return Z}


New main under 6.com, new Main.go
7.main.go input

Package Mainimport ("com/sqrt" "FMT") func main () {FMT. Printf ("Hello, World.  ") SQRT (2) =%v\n ", Sqrt. SQRT (2))}

8. Command line switch to SRC, enter command
Go Install com/sqrt
This time a PKG is generated under the root Goproject

Input command under 9.SRC
Go Install Com/main
Compile build Main (main.exe) command line

10. Execution./main (Main.exe) there's the output.

Go of this kind of contract is concise, actually goroot is equivalent (java_home)
Gopath equivalent to path, dependent paths, can be configured multiple, Linux separated by a colon (:), window with a semicolon (;)
Go is a one-pack/two-level package/package name like Com/sqrt



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.