Go (also known as golang [ 3] ) is Google a development of static strongly typed , compiled , and hair , and has a garbage collection function of the programming language . From Wikipedia
1. What is the Go language?
Go is a concurrency support, garbage collection and compilation systems compiled languages, aimed at creating a door has has a good balance between high performance and efficient development of dynamic languages statically compiled language, a programming language.
Go language is Rob Pike and Ken Thompson in September 2007 to start the design, Go is based on the Inferno operating system development. [ 4] Go in November 2009 officially announced to become open-source project, and Linux and Mac OS X has been achieved on the platform, and later added the realization of a Windows system. (This is similar to the marmot logo Rob Pike's wife Renee French drawing)
2. Go language advantage
Value Go Language: Go in Google: software engineering for the purpose of language design
Go main features are the following:
- 1, type-safe and memory safe
- 2, in a very intuitive and very low cost program to achieve high concurrency
- 3, efficient garbage collection mechanism
- 4, fast compiler (while addressing the problem of too many C language header file)
- 5, provides for the performance of a multicore computer program
3.Go language of architecture project
Go language working directory in the following table, according to the agreement, GoPath
the next need to create three directories
table of Contents | meaning |
---|---|
bin | Generated after storing the compiled executable file |
pkg | After storing the generated files to compile the package |
src | Source storage project |
GOPATH is your working directory, GOROOT your installation directory.
The overall structure of the program as shown below:
4.Go language development environment
This tutorial uses go1.9
and goland 2018.1
development environment, we must first learn to environment configuration (not talked about here, it is relatively simple), and then begin a tour of the development of Go!
1. Create a new HelloWord.go
file
Right in the new time will be two file formats:
Empty file
After the new file is empty, then Simple Application
the new will main
sample code function.
package main
import "fmt"
func main() { fmt.Print("hello,world!") }
Go the general structure of the program:
- 1, Go through the program
package
organized; - 2, only the
package
namemain
of the package may include a main function, and only an executable program has a main package; - 3, through
import
to the main package introducing other non-keyword; - 4, by
const
definition constant keyword; - 5, by using an external function body
var
to declare global variables and assign keywords; - 6, by
type
or interface (interface) to a statement structure (struct) keywords; - 7, through
func
to declare the function key.
Results are as follows:
2. Basic type
The basic type of language go as follows:
Examples are as follows:
package main
import (
"fmt"
"strconv"
)
func main() { //声明和赋值 var a int = 65 var c int = 75 //简化赋值和显示强制转换 b:= string(a) //转换为数字 d:= strconv.Itoa(c) fmt.Println(b) fmt.Println(d) }
The results are as follows:
Because the string () said it will convert the data to text format, nature and therefore anything stored in the computer is digital, so this function is natural to think that what we need is a text representation of 65 A.
3. Functions
Call the function as follows: References Go Guide Chinese version
//func 方法名 形式参数 返回参数类型
func add(x int,y int) int { return x+y } //func add(x ,y int) int { // return x+y //} func main() { fmt.Print(add(42,13)) }
4. Back multivalued
package main
import "fmt"
func main() { a,b:=swap("hello","world") fmt.Print(a,b) } func swap(a string, b string) (string,string) { //这个可能是交换程序了,甚至不需要不需要一个临时变量,但是这个内部是怎么做的呢? return b,a }
Q: go language swap method is how to do it?
5. Use of the variables and variable short
package main
import "fmt"
var i,j int =1,2 func main() { var c, python, java = true,false,"no" //短变量的声明和赋值 k:=3 fmt.Print(i,j,c,python, java,k) }
6. The types of the initial value
package main
import "fmt"
func main() { var i int var f float64 var b bool var s string fmt.Printf("%v %v %v %q\n", i, f, b, s) }
7. Type conversion (conversion must be displayed, not implicit conversions in Go)
package main
import (
"math"
"fmt"
)
func main() { var x, y int = 3, 4 //将xy的值强制类型转换为float64类型 var f float64 = math.Sqrt(float64(x*x+y*y)) //unit无符号整数 var z uint = uint(f) fmt.Print(x,f,z) }