The function is the core design of Go inside, it declares by the keyword func, its format is as follows: Func funcName (input1 type1, Input2 type2) (output1 type1, Output2 type2) {//This is the processing logic generation Code//Return multiple values return value1, value2} The above code we see • The keyword func is used to declare a function funcname function can have one or more parameters, each parameter is followed by a type, through, delimited • Function can return multiple values · The above return value declares two variables output1 and output2, and if you do not want to declare it, you can do it directly on two types • If you have only one return value and do not declare a return value variable, you can omit the parentheses that include the return value • If there is no return value, omit the last return message directly. If there is a return value, then the return statement must be added to the outer layer of the function Func max (A, b int) int {If a > B {return A}return B} If your function is exported (capitalized), the official recommendation: It is better to name the return value because it is not named return value, although it makes the code more concise, but will cause the resulting document readability poor. Func sumandproduct (A, B int) (add int, multiplied int) {add = A+bmultiplied = A*breturn} The argument go function supports arguments. The function that accepts the argument is a variable number of arguments. To do this, you first need to define the function to accept the argument: the func myfunc (arg ... int) {}arg ... int tells Go that the function accepts an indefinite number of arguments. Note that the types of these parameters are all int. In the function body, the variable arg is an int of slice:for _, N: = range arg {fmt. Printf ("and the number is:%d\n", N)} pointer when we pass a parameter value into the called function, we actually pass a copy of the value, and when the parameter value is modified in the called function, the corresponding argument in the calling function does not change. Because the numeric changes only work on copy. This involves the so-called pointers. We know that variables are stored at a certain address in memory, and modifying a variable is actually the memory at the address of the modified variable. Only the ADD1 function knows the address where the x variable is located to modify the value of the X variable. So we need to putThe X address &x the function and changes the type of the function's arguments from int to *int, which is the pointer type, in order to modify the value of the x variable in the function. At this point the parameter is still passed by copy, except that the copy is a pointer. Take a look at the following example package Mainimport "FMT"//simple function that implements the operation of parameter +1 func add1 (a *int) int {///note, *a = *a+1//modified a value return *A//return new value} Func Main () {x: = 3fmt. Println ("x =", x)///should output "x = 3" x1: = ADD1 (&x)//Call ADD1 (&X) to the address of the X. FMT. Println ("x+1 =", x1)//should output "x+1 = 4" FMT. Println ("x =", x)//should output "x = 4"} so that we have achieved the purpose of modifying X. So what are the benefits of passing pointers? • Passing pointers allows multiple functions to manipulate the same object. • The pointer is relatively lightweight (8bytes), just the memory address, we can pass the pointer to the large volume structure. If passed with a parameter value, a relatively large amount of overhead (memory and time) is spent on each copy. So when you're passing large structures, it's a wise choice to use pointers. Go language String,slice,map These three types of implementations are similar to pointers, so they can be passed directly without taking the address and passing pointers. (Note: If the function needs to change the length of the slice, the address pass pointer still needs to be taken)
First knowledge Go (3)