A summary of the way to build the Go language

Source: Internet
Author: User
Tags manual writing naming convention square root

Taking advantage of the recent time to change work, looked at the go language, compared with C + +, the Go language is indeed a lot of light in many places, such as: added the built-in string type, multiple return values, support, simple construction method and so on. Make a lot of improvement in production efficiency. Here is a simple summary of how the go language is built today.

In a C + + project, very little use of a single command to compile code, usually through a number of tools to automate the compilation, at the beginning of the manual writing makefile, and then the complicated autotools, then appeared cmake, according to the passage of time, We have less work to do, for example, in Autotools we generally need to work as follows:

    1. AutoScan scans the working directory, and then manually modifies the generated configure.ac.
    2. Use the aclocal command to generate ACLOCAL.M4 through CONFIGURE.AC.
    3. Use the autoconf command to generate a configure script.
    4. Use the Autoheader command to generate the config.h.in.
    5. Create the makefile.am file manually, configure it according to your project needs, and then use the Automake command to generate the makefile.in file.
    6. Execute the Configure script again, and finally generate the makefile.

After completing the above steps, you can make,make install to complete the installation of the project compile. And then the cmake is a lot easier, only need to configure a few CMakeList.txt, then execute the cmake command, you can build and install the required makefile files.

Although with the development of technology, C + + will have a better way to build. But at present, is not able to get rid of makefile, and in Go1 release, abandoned the makefile, directly introduced a more convenient method: **go command line tool * *.

The Go command line tool discards the concept of engineering files, only through the directory structure and the name of the package to knock down the engineering structure and build order, below we use a simple example (from "Go Language Programming") to illustrate the basic engineering management method in go.

This example is a command-line based calculator. The basic usage is as follows:

$calc helpusage:calc command [argument] ... The Command are:sqrt Square root of a non-4  #对4进行开方 212< /c7>3

According to the requirements, we can divide the project into two parts, the main program and the algorithm library, so that when we update the algorithm, we can only modify the implementation, without modifying the external interface, so that can achieve low coupling. The project catalog is as follows:

Calculator
|----src
|----Calc
|----Calc.go
|----Simplemath
|----Add.go
|----Add_test.go
|----Sqrt.go
|----Sqrt_test.go
|----Bin
|----pkg

On top, italic represents the directory, the normal article represents the file, and **xx_test.go represents the unit test file for Xx.go * *, which is the naming convention for Go engineering. At the same time, the project under the catalog src is the source directory, bin represents the installed executable directory, PKG represents the package directory, this is the go Project naming rules. Here is the code for this project.
Calc.go

Calc.go
Package Main
Import (
"FMT"
"OS"
"Simplemath"
"StrConv"
)
var Usage = func () {
Fmt. Println ("Usage:calc command [arguments] ...")
Fmt. Println ("\nthe commands is: \n\taddition of values.\n\tsqrt\tsquare root of a non-negative value")
}
Func Main () {
args: = os. Args
if args = = Nil | | Len (args) < 2 {
Usage ()
Return
}
Switch Args[0] {
Case "Add":
If Len (args)! = 3 {
Fmt. Println ("Usage:calc add <integer1> <integer2>")
Return
}
V1, err1: = StrConv. Atoi (Args[1])
V2, ERR2: = StrConv. Atoi (Args[2])
If err1! = Nil | | Err2! = Nil {
Fmt. Println ("Usage:calc add <integer1> <integer2>")
Return
}
RET: = Simplemath. ADD (v1, v2)
Fmt. Println ("Result:", ret)
Case "sqrt":
If Len (args)! = 2 {
Fmt. Println ("Usage:calc sqrt <integer>")
Return
}
V, err: = StrConv. Atoi (Args[1])
If err! = Nil {
Fmt. Println ("Usage:calc sqrt <integer>")
Return
}
RET: = Simplemath. Sqrt (v)
Fmt. Println ("Result:", ret)
Default
Usage ()
}
}

Add.go

1 // add.go2package Simplemath34intint int {5     return a + b6 }

Add_test.go

1 // add_test.go  2packageSimplemath  3  4 Import "testing" 5
     6 func TestAdd1 (T *testing. T) {  7     r: = ADD (1, 2)  8     if r! = 3 {  9
    
     t.errorf ("Add (1, 2) failed, Got%d, expected 3"
     , R)
     ten
         }
     
    

Sqrt.go

1 // sqrt.go2 packagesimplemath34 import "math"5  6intint  {7     V: = Math. Sqrt (Float64 (i))8     int(v)9 }

Because of the space problem, sqrt's unit test code is omitted here.

After the code is finished, it is compiled. First, you need to set the environment variable **gopath** value, the **calcuator directory to gopath**, save and reload. Assuming that the Calcuator directory is "~/gobuild", the following commands can be executed under Linux:

Export gopath=~/gobuild/~/.BASHRC

After setting the environment variables, you can start building the project, enter the Calcuator directory, and execute the command:

CD Bingo Build Calc

The executable program named Calc can then be found under the directory. Experiment with the previous function, that is, you can see the corresponding execution results.

This is the go to build the process, according to the requirements of go to organize the directory, the real build is just **go build calc** this command. Can be said to be very simple and fast. Also, for unit testing, execute the command in the bin directory:
go Test Simplemath .

The above is a simple summary of the go build. Because it is just beginning to touch the go language, if there is a mistake, please correct me. Thank you
Xiaoniu
[2/30]

Resources:

Go Language programming

A summary of the way to build the Go language

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.