go: Built-in functions | closures | array | slice | sort | map | Lock

Built-in functions 
1.close: mainly used Close Channel 
2.len: seeking to length, such as String, Array, Slice, Map, Channel 
3.new allocate memory and are used to make 
	new types of values for dispensing, such as int, string, returns a pointer to 
	make a reference to the type of distribution, such as chan, map, slice 
	pointers point to create new variables, variables are initialized make 
	
4.append to append to the array elements, slice in 
5.panie and recover , used for error handling 

closure 
1. And a combining function associated therewith drink from environmental entities 
	Package main 

	Import "FMT" 
	FUNC Addr () FUNC (int) {int 
		var X int 
		return FUNC (D int) {int 
			X = D + 
			return X 
		} 
	} 
	FUNC main () { 
		F: Addr = () 
		fmt.Println (F (. 1)) 
		fmt.Println (F (100)) 
		fmt.Println (F (1000)) 
	} 

array
 
1. array:
	Is a sequence of fixed length of the same data type 
2. The array defined 
	var A [len] int 
3. length of the part of the data type, therefore, var a [5] int and var a [10] int different types 
4. Data can be accessed by index, index starts from 0, the last element of an index. 1-len 
5. the access violation, if the array index is outside the legal range, the access violation trigger, will panic 
6. the array initialization 
	a.var Age () [. 5] int = [. 5] int {1,2,3,4,5} 
	b.var Age () = [. 5] int {1,2,3,4,5} 
	C Age = .var [...] {1,2,3,4,5} int 
	d.var STR = [. 5] {String. 3: "Hello World",. 4: "Tom"} 
7. The multi-dimensional array 
	a. Age var [. 5] [. 3] int 
	b.var F [2] [. 3] = int [...] [. 3] int {{l, 2,3}, {4,5,6}} 

slice 

1. slice, the slice is a reference to an array slice is a reference type and therefore 
the length of the sections 2 can be varied, therefore, is a variable array slice 
3 and slice array traversal as possible () with the required length len 
4.cap can maximum capacity determined slice, 0 <= len (slice) <= cap (array), wherein the reference array arrayshi slice 
defines a slice of 5
	var variable name [] types, such var str [] string var arr [ ] int 
initialization 6. slice: 
	var Slice [] int = ARR [Start: End] 
	var Slice [] int = ARR [0: End] abbreviation var Slice [] int = ARR [:] 
	var Slice [] int = ARR [Start: len (ARR)] abbreviation var Slice [] int = ARR [Start:] 
	var Slice [] int = ARR [0, len ( ARR)] slice abbreviation var [] int = ARR [:] 
7. The slices created by the make 
	var slice [] type = the make ([] type, len) 
	slice: = the make ([] type, len) 
	slice: = the make ([] type, len, CAP) 
8. The operation of the slice with built-in functions append 
	slice = append (slice, 10) 
	var A = [] {l, 2,3} int 
	var B = [] int {4,5,6 } 
	A = the append (A, B ...) 
9. The copy sections 
	S1: = [] int {1,2,3,4,5} 
	S2: = the make ([] int, 10) 
	copy (S2, S1) // copy will not expansion, append will. 
	S3: = [] int {1,2,3}
	the append = S3 (S3, S2 ...) 
	S3 = the append (s3,4,5,6) 
10. The modified character string 
	S: = "Hello World" 
	S1: = [] Rune (S) // [] byte (S) 
	S1 [0] = '0' 
	STR: = String (S1) 
	
sorting 
1.sort.Ints () to sort the certificate 
2.sort.String sort of character and channeling 
3.sort.Float64s to float Sort 
4.sort.SearchInte (a [] int, b int) b from lookup array a. The proviso that a orderly 
5.sort.SearchFloats (a [] float64, b float64) b from lookup array a. The proviso that a orderly 
7.sort.SearchStrings (a [] string, b string) from the array a lookup b. Ordered proviso that a 





map 
data structure of 1.key-value, also known as a dictionary or an associative array 
	a. Statement 
		var map MAP1 [KeyType] ValueType 
		var a map [String] String 
		var a map [String] int 
lock 
		var a map [int ] string
		A Map var [String] Map [String] String 
		// declare no memory is allocated, initialization requires the make
	


1. mutex 
	sync.Mutex 
	var Lock sync.Mutex 
	FUNC Test () { 
		var A Map [int] int 
		A = the make (Map [int] int) 
		A [. 7] = 10 
		A [. 5] = 10 
		A [. 3 ] = 10 
		A [. 1] = 10 
		for I: = 0; I <. 3; I ++ { 
			Go FUNC (B Map [int] int) { 
				Lock.lock () 
				B [. 8] = rand.Intn (100) 
				Lock. UNLOCK () 
			} (A) 
			Lock.lock () 
			fmt.Println (A) 
			lock.unlock () 
			the time.sleep (time.Second) 
		} 
	} 
2. write lock 
	var MU sync.RWMutex 
	Go detection code directory Build RACE Is there a conflict

  

Guess you like

Origin www.cnblogs.com/kakawith/p/12584528.html