This is a creation in Article, where the information may have evolved or changed.
Run: Go run xxx.go
Compile: Go build xxx.go Generate executable file
Program Start: Package Main
Introduction Package: Import "FMT" or import (
"FMT"
"OS"
)
Note: println does not support, printf only supports the output of the% type
Variables and constants:
Declare variables outside of the function: Var a or Var (
a int
b str = "HI"
)
Used in functions:: = declaration, e.g. T: = 10
Note: In go, declaring a variable can not declare a type, it can be inferred automatically by value
Constant definition: const s string = "hi" or const with the Const keyword (
s string = "Hi"
Pi float32 = 3.1415926
)
Array:
var A [5]int defines a value of length 5 as an array of type int
var b [3]int{1, 2, 3} defines a length of 3, and a result of a value of type int is = "[1, 2, 3]
Returns the length of an array using the Len () method
Array slicing operations:
var a [5]int
or a: = [5]int{0,1,2,3,4}
A[2:4] from 2 to 4, excluding 4
A[:4] from 0 to 4, excluding 4
A[2:] from 2 to last including 2
Conditional statements:
If Len (a) = = 5 {....} else {...} Switch a {case a[0] >0: .... case a[2] = = 2: .... Default: .... }
Note that the switch statement in the go language is not break because Casa in go does not wear
Loop statement:
For I: = 0; I < 5; i++ {} range keyword loop array, map, etc. i: = [3]int{2, 3, 4} for a, V: = Range I {fmt. Println (a) fmt. Println (v)} Dead loop: for {}
Map
Package mainimport "FMT" func main () { m := make (map[string]int) //use make to create an empty map m["One"] = 1 m["] = 2 m[" "three"] = 3 fmt. Println (m) //output map[three:3 two:2 one:1] (the order may not be the same at run time) fmt. Println (Len (m)) //output 3 v := m["] //" value from map fmt. Println (v) // output 2 delete (M, " fmt"). Println (m) //output map[three:3 one:1] m1 := map[string ]int{"One": 1, "one" and ": 2, " "three": 3} fmt. PRINTLN (M1) //output map[two:2 three:3 one:1] (sequence may not be the same at run time) for key, val := range m1{ fmt. Printf ("%s => %d \n", key, val) /* Output: (may be different in order at run time) three => 3 one => 1 two => 2*/ }}
Pointer
var i int= 1var pInt *int= &i//output: I=1 pint=0xf8400371b0 *pint=1fmt. Printf ("i=%d\tpint=%p\t*pint=%d\n", I, PInt, *pint) *pint = 2//output: i=2 pint=0xf8400371b0 *pint =2fmt. Printf ("i=%d\tpint=%p\t*pint=%d\n", I, pInt, *pint) i = 3//output: i=3 pint=0xf8400371b0 *PINT=3FM t.printf ("i=%d\tpint=%p\t*pint=%d\n", I, PInt, *pint)
Go has two mechanisms for allocating memory, namely the built-in function new and make. They do things differently, they apply different types, which can cause confusion, but the rules are simple.
Memory allocation
New is a built-in function that allocates memory, but unlike the work of new in other languages with the same name, it simply zeroed out the memory instead of initializing the memory. New (t) assigns a zero-value storage space and returns the address, which is a value of type *t, to a project of type T. In the terms of go, it returns a pointer to the newly assigned 0 value of type T.
The purpose of the make (T, args) function differs from new (t). It is used only to create slices, maps, and Chan (message Pipelines) and returns an initialized (not 0) instance of type T (not *t). This difference arises because these three types are essentially references to data structures that must be initialized before they are used. For example, a slice is a descriptor with three items of content, including pointers, lengths, and capacities that point to data (inside an array), and the slice value is nil until the three items are initialized. For slices, mappings, and channels, make initializes its internal data structure and prepares the values that will be used. Such as:
The following code assigns an integer array with a length of 10, a capacity of 100, and a slice that returns the first 10 arrays
Make ([]int, 10, 100)
以下示例说明了new和make的不同。
VarP *[]int = new ([]int)//Allocate memory for the tile structure; *p = nil; rarely use varv []int = make ([]int, 10)//Slice v now is a reference to a new array with 10 integers//unnecessarily complicates the problem *[]int: var p = new ([]int) fmt. PRINTLN (p)//output: &[]*p = make ([]int, Ten, ten) fmt. PRINTLN (p)//output: &[0 0 0 0 0 0 0 0 0 0]fmt. Println ((*P) [2])//output: 0//Customary usage: V: = make ([]int) fmt. Println (v)//output: [0 0 0 0 0 0 0 0 0 0]
The above content is the ordinary Childe himself some of the study notes and summaries, thanks to the study of the author of the articles read!