Go language 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, the previous period of time 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 this tutorial https://golang.org/doc/ Code.html, I follow my own understanding of the record, English good children's shoes or read the original bar!


The go language can be makefile, or it can be compiled directly according to his conventions. The Convention is actually relatively simple, the go language is so to configure his project

prjdir# Project root directory bin/hello              # executable file, window under the hello.exepkg/# package with directory linux_amd64/# Execute Platform example/# sub-package directory newmath.a  # package, Similar to Java in the jarsrc/# source code example/hello/# Similar to Java Example.hello,java is the package internal directory, go in the package outside the Hello.go   # command line, this must be in the main package, There is a main function, which generates the name hellonewmath/# package name, will be located in example, build NEWMATH.A package sqrt.go    # package source code file, 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 Prjdir directory under D:\workspace

2. Setting Gopath Environment variables

Gopath = D:\workspace\prjDir

New SRC under 3.prjDir

4.SRC New example, new Newmath and Sqrt.go files

5.sqrt.go File Input Code

Package Newmath are a trivial example package.package newmath//Sqrt returns an approximation to the square root of X.fu NC Sqrt (x float64) float64 {        //This is a terrible implementation.        Real code should import "math" and use MATH.SQRT.        Z: = 0.0 for        i: = 0; i <; i++ {                Z-= (z*z-x)/(2 * x)        }        return Z}


New Hello under 6.example, new Hello.go

7.hello.go input

Hello is a trivial example of a main package.package mainimport (        "Example/newmath"        "FMT") func main () {        Fmt. Printf ("Hello, World.  ") SQRT (2) =%v\n ", Newmath. SQRT (2))}

8. Command line switch to SRC, enter command

Go Install Example/newmath

At this time, the root prjdir generated a pkg, content such as


Input command under 9.SRC

Go Install Example/hello

Compile build Hello (hello.exe) command line


10. Execution./hello (Hello.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 reference is   level pack/two-level package/package name such as Example/newmath

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.