Built-in functions 1, pre-defined a few functions, which means that you can use them close for channel communication without referencing any packages. Delete is used to remove instances in the map Len and cap can be used for different types, and Len returns the lengths of strings, slice, and arrays. New is used for various types of memory allocations. Make is used for memory allocations of built-in types (map, slice, and channel). Copy is used to replicate slice. Append is used to append slice. Panic and recover are used for exception handling mechanisms. Print and println are the underlying print functions that can be used without the introduction of a FMT package. They are primarily used for debugging. Complex, real, and imag are all used to process complex numbers. can use array to sort multiple values in a list, or use more flexible: slice. A dictionary or hash type can also be used, which is called map in Go. 2. Arrayarray is defined by [n]<type>, n indicates the length of the array, and <type> identifies the type of content that you want to store. The assignment or index of an element to an array is done by square brackets: var arr [10]intarr[0] = 42arr[1] = 13fmt. Printf ("The first element is%d\n", arr[0]) like var arr = [10]int Such an array type has a fixed size. The size is part of the type. Because different sizes are different types, you cannot change the size. Arrays are also value types: assigning an array to another array will copy all the elements. Especially when passing an array to a function, it gets a copy of the array, not the pointer to the array. You can declare an array like this: Var a [3]int, if you do not initialize it with zero, then use a compound declaration: A: = [3]int{1, 2, 3} can also be abbreviated as a: = [...] Int{1, 2, 3},go automatically counts the number of elements. Note that all items must be specified. So, if you use a multidimensional array, there is something you must enter: a: = [2][2]int{[2]int{1,2}, [2]int{3,4}} similar to: a: = [2][2]int{[...] int{1,2}, [...] int{3,4}} When declaring an array, you must enter something in square brackets, a number or three dots (...)。 3, Sliceslice, and array are close, but can increase in length when new elements are added. Slice always points to an array at the bottom. Slice is a pointer to an array, which is different from the array; Slice is a reference type, which means that when you assign a slice to another variable, two references point to the same array. For example, if a function requires a slice parameter, modifications to the slice element in it will also be reflected in the function caller, similar to passing the underlying array pointer. By: SL: = Make ([]int, 10) Creates a slice that holds 10 elements. It is important to note that the underlying array is not different. Slice always appears in pairs with a fixed-length array. It affects the capacity and length of the slice. First, an array of M element lengths is created, the element type Int:var array[m]int and then the Slice:slice is created for this array: = Array[0:n] and now:? Len (slice) = = N;? Cap (slice) = = m;? Len (array) = = Cap (Array) = = M 4, map has a map type in Go. Map can be thought of as an array indexed by a string (in its simplest form). The following defines the map type, which is used to convert a string (the abbreviation of the month) to the number of days int– that month. The general way to define the map is: Map[<from type>]<to type>monthdays: = map[string]int{"Jan": "Feb": 30, "Mar":---- "May": +, "June": +, "Jul":---------"SEP": +, "Oct": +, "Nov": +, "Dec": 31,← comma is a must} note that when you only need to declare a map, use make Form: monthdays: = Make (Map[string]int) when indexed (searched) in map, square brackets are used. For example, print out the number of days in December: FMT. Printf ("%d\n", monthdays["Dec"]) when the array, slice,When a string or map loop is traversed, range will help you, and each time it is called, it will return a key and the corresponding value. year: = 0for _, Days: = Range Monthdays {← Key is not used, so use _, daysyear + = days}fmt. Printf ("Numbers of Days in a years:%d\n", year) adds elements to the map, which can be done: monthdays["UNDECIM"] = 30← add one months monthdays["Feb"] = 29← to rewrite this element check if the element exists, you can use the following method: Var value intvar present boolvalue, present = monthdays["Jan"]← if present, present has value TR ue← or closer to Go mode V, OK: = monthdays["Jan"]← "comma ok" form You can also remove elements from the map: Delete (monthdays, "mar") ← Delete "Mar" It's always raining month usually The statement Delete (m, x) deletes the instance established by M[x] in the map. function 1, function is the basic part of Building Go program type MyType int← new types, see 5th func (P mytype) funcname (q int) (r,s int) {return 0,0}&N The function has no return value: Func subroutine (in int) {return} simply returns the input: Func identity (in int) int {return in} recursive function: Func rec (i int) {if i = = ten {return}rec (i+1) fmt. Printf ("%d", I)} This will print: 9 8 7 6 5 4 3 2 1 0. &NBSP;2, scoped variables that are defined outside the function are global, and those defined inside the function are local to the function. If naming overrides-a local variable has the same name as a global variable-the local variable overrides the global variable package mainvar a IntFunc main () when the function executes{a=5println (a) f ()} func F () {A: = 6println (a) g ()}func g () {println (a)} output will be: 565. A local variable is only valid when executing a function that defines it 3, multivalued return functions, and methods can return multiple values, func nextint (b []byte, I int) (int, int) {x: = 0//assumes all numbers are numeric for; I < L En (b); i++ {x = x*10 + int (b[i])-' 0 '}return x, i} You can scan the numbers in the input array like this: a: = []byte{' 1 ', ' 2 ', ' 3 ', ' 4 '}var x intfor i: = 0; I < Len (a); {← No i++x, i = Nextint (A, I) println (x)} 4, named return value??? &NBSP;5, delay code??? Suppose you have a function that opens the file and reads it a few. In such a function, there is often a place to return early. If you do this, you need to close the working file descriptor. The defer statement. The function specified after defer is called before the function exits. For I: = 0; I < 5; i++ {defer FMT. Printf ("%d", I)} Deferred functions are executed in last in, first out (LIFO) order, so the above code prints: 4 3 2 1 0. Access return value in defer func f () (ret int) {←ret initialized to 0defer func () {Ret++←ret increments to 1} () return 0← returns 1 instead of 0}&NBSP;6, variable parameter accept variable The function is a func myfunc (arg ... int) with an indefinite number of arguments {}arg ... int tells Go that the function accepts an indefinite number of arguments 7, function as a value function is also a value anonymous function func main () {A: = Func () {← Defines an anonymous function, and assigns a value to Aprintln ("Hello")}← there is no () a () ← Call Function} if using FMT. Printf ("\%t\n", a) prints the type of a, and the output is func (). 8, callbacks When a function is a value, it can be easily passed to other functions, and then it can be used as a callback. First, define a function that does something for the integer: Func printit (x int) {← Function has no return value for FMT. Print ("%v\n", x) ← Just prints} the function's identity is func printit (int), or there is no function name: func (int). Create a new function using this as a callback, you need to use this identifier: Func callback (y int, f func (int)) {←f will save the function f (Y) ← Call the callback function f input variable y}
Functions of the Go Language Foundation (c)