First, the concept
function allows us to package a sequence of statements into a single unit, which can then be called multiple times from other parts of the program. The mechanism of the function allows us to break a big job down into small tasks that allow different programmers to perform independently at different times and places. A function hides its implementation details from the user at the same time. Because of these factors, functions are a vital part of any programming language.
Ii. Declaration and use of functions
Package Mainimport ( "fmt") Func main () { say (" Hello"string"{ fmt. Println ("Say:", info)}
Three, multiple return value functions
Package Mainimport ( "fmt") Func main () { ID, name:Get () Get() (intstring) { return1 "frod"}
Iv. Anonymous functions
Package Mainimport ("FMT") Func main () {//passing an anonymous functionRes: = SUM (func () (int,int) {return 1,2}) fmt. Println (RES)}//anonymous functions as arguments to functionsFunc sum (f func () (int,int))int{A, B:=f ()returnA +B}
Five, anonymous function-closure function
Package Mainimport ( "fmt") Func main () { // sum () Inside is a closure function, the closure function is immediately executed res: = SUM (func () (intintreturn12 } ()) intint { return A + B}
Six, anonymous function-nested, meaning is not particularly large, just for basic learning
Package Mainimport ( "fmt") Func main () { res:int { A, B:= func () (intintreturn12 } () return A + b } () FMT. Println (RES)}
Vii. functions-Variable parameters
package mainimport ( " fmt " ) func main () {FMT. Println (SUM ( 1 , 2 , 3 , 4 , 5 , 6 int ) int { var sum int for V: = range vals {sum +=< Span style= "COLOR: #000000" > v} return sum}
Golang function Note