Get started with the go Language

Source: Internet
Author: User
Tags install go
The main features of the Go language:
? Automatic garbage collection
? Richer built-in types
? Multiple function return values
? Error Handling
? Anonymous functions and closures
? Types and interfaces
? Concurrent Programming
? Reflection
? Language Interaction

1.2.4 error handling
The Go language introduces three keywords for the standard error handling process, which are defer, panic, and
Recover.


1: Before the compilation environment is ready for release in go 1, developers can only download the code and compile it on their own. Now they can download the corresponding installation package for installation. The installation package is http://code.google.com/p/go/downloads/list. In * nix environment, go is installed in the/usr/local/go directory by default. After installation is complete, the installation package automatically adds the execution file directory to the system path.

Install go on Ubuntu:
Sudo add-Apt-repository PPA: gophers/go
Sudo apt-Get update
Sudo apt-Get install golang-stable

Or

Sudo apt-Get install golang

Or download the go language installation package directly.

Http://blog.csdn.net/liuhongwei123888/article/details/8512815

// Calc. gopackage mainimport "OS" // used to obtain the command line parameter OS. argsimport "FMT" import "simplemath" import "strconv" Var usage = func () {FMT. println ("Usage: calc Command [arguments]... ") FMT. println ("\ nthe commands are: \ n \ Tadd \ taddition of two 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 ()
}

Code List 1-6
Add. Go
// Add. Go
Package simplemath
Func add (A int, B INT) int {
Return A + B
}

Code List 1-7
Add_test.go
// Add_test.go
Package simplemath
Import "testing"
Func testadd1 (T * testing. t ){
R: = add (1, 2)
If R! = 3 {
T. errorf ("add (1, 2) failed. Got % d, expected 3.", R)
}
}
Code List 1-8 SQRT. Go
// SQRT. Go
Package simplemath
Import "math"
Func SQRT (I INT) int {
V: = math. SQRT (float64 (I ))
Return int (V)
}
Code List 1-9 sqrt_test.go
// Sqrt_test.go
Package simplemath
Import "testing"
Func testsqrt1 (T * testing. t ){
V: = SQRT (16)
If V! = 4 {
T. errorf ("SQRT (16) failed. Got % v, expected 4.", V)
}
}

To be able to build this project,
Add the root directory of the project to the environment variable gopath.
Assume that calcproj
The directory is located in ~ /Goyard, edit ~ /. Bashrc file, and add the following line of code:
Export gopath = ~ /Goyard/calcproj
Run the following command to apply the setting:
$ Source ~ /. Bashrc
The gopath and PATH environment variables are the same. They can also accept multiple paths and are separated by colons.
After setting gopath, we will start to build the project. Suppose we want to put the generated executable file
In the calcproj/bin directory, the following commands must be executed:

Cd ~ If the/Goyard/calcprojmkdir bincd bingo build calc succeeds, an executable file named calc is found in this directory, and the file is executed to view help information and perform arithmetic operations: $. /calcusage: calc Command [arguments]... the commands are: addaddition of two values. sqrtsquare root of a non-negative value. $. /calc add 2 3 result: 5 $. /calc SQRT 9 result: 3 from the above build process, we can see that the real build command is like: Go build calc

 

Nn

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.