This is a creation in Article, where the information may have evolved or changed.
What is go? From the website:
The Go programming language is an open source project that makes programmers more efficient. Go is expressive, concise, clear, and efficient. Its parallel mechanism makes it easy to write multicore and web applications , while the novelty type system allows for the building of resilient modular programs. Go compiles to machine code very quickly, with convenient garbage collection and powerful runtime reflection . It is a fast, statically typed compilation language, but it feels like a dynamic type, interpreted language.
First, we introduce the features of the Go language--
- Parallel: Go makes the function very easy to be a very lightweight thread. These threads are called goroutines in go;
- Channel: The communication between these goroutines is completed by Channe;
- Fast: Compiles quickly and executes quickly. The target is as fast as C. Compile time is calculated in seconds;
- Security: When converting one type to another, explicit conversions are required and strict rules are followed. Go has garbage collection, no free () in Go, language will handle all of this;
Second, introduce the package in the go language, how to get a built-in package information. Can be--go Doc fmt, go doc Hash.
Let's go into the Hello world~! of Go World
1 Package Hello // If you change to package main, you can 23 import "FMT"45 func Main () {6 FMT. Printf ("Hello world\n")7 }
The above program cannot be run for three reasons:
- All go files start with package <something> and must be package main for standalone run files;
- This means that the FMT package needs to be added to main. Other packages that are not main are called libraries
- The package main must appear first, followed by import. In go, the package always appears first, then import, then everything else. When the Go program executes, the function that is called First is Main.main (), which is inherited from C. This function is defined here;
A single statement in the go language that automatically ends with a semicolon, but if you want one or more statements on the same line, remember to add a semicolon between the statements. Otherwise, it cannot be compiled and executed.
1 Package Main 2 import "FMT"34int =5 //b: = Ten the way this declaration and assignment is joined is not allowed outside of the function 6func main () {7 A: = 15//function internal can use 8 FMT. Printf ("Hello World%d%d\n", A, b)9 }
Multiple var declarations can be made into groups, and const and import also allow this. Notice the use of parentheses:
VAR (
x int
b BOOL
)
Multiple variables of the same type can also be declared in one line: var x, y int let x and y are both int type variables. You can also use parallel assignments:
A, B: = 20, 16
The Go compiler has an error in declaring a variable that is not used.
Go has a well-known type such as int, and this type determines the appropriate length based on your hardware. means 32 bits on 32-bit hardware and 64 bits on 64-bit hardware. Note: int is one of 32 or 64 bits and is not defined as a different value. UINT in the same situation. If you want to be clear about its length, you can use Int32 or UInt32. The complete list of integer types (symbols and unsigned) is Int8,int16,int32,int64 and Byte,uint8,uint16,uint32,uint64. Byte is the alias of Uint8. The values for floating-point types are float32 and float64 (no float type). 64-bit integers and floating-point numbers are always 64-bit, even on 32-bit architectures.
Enumeration Type--
Const (
A = iota//The first iota is represented as 0, so a equals 0, and when Iota is used again on a new line, its value increases by 1, so the value of B is 1.
b = Iota
c = Iota
)
String--
A string in Go is a sequence of characters wrapped in UTF-8 by double quotation marks ("). If you use single quotation marks ('), a character is represented.
Control statements:
1 if <--{is mandatory//no parentheses are required, but if the first {must be accompanied by 2 return y3} else { 4 return x5 }
Go's for Loop has three forms, only one of which uses semicolons.
For Init; Condition Post {} and C for the same
For condition {} and while
for {} and C for (;;) (dead Loop)
when looping through nested loops, you can specify a label after break. Use the label to determine which loop is terminated: j:for J:= 0; J < 5; J + + {for i:= 0, i <; i++ {if i > 5 {break J now terminates Is the J Loop, not the one of I } println (i)} }
1 It does not match the failure automatically after the attempt is made, but you can use Fallthrough to make it do so. Didn't2 There are fallthrough:3 switch I {4 Case0://The empty case body5 Case1:6F ()//when i = = 0, F will not be called! 7 }8 and this:9 switch I {Ten Case0: Fallthrough One Case1: AF ()//when i = = 0, F will be called! - } - Use default to specify the behavior when all other branches do not match. the switch I { - Case0: - Case1: - f () + Default: -g ()//when I is not equal to 0 or 1 o'clock call +}