This is a creation in Article, where the information may have evolved or changed.
For the definition or understanding of the Golang function:
function is the smallest modular unit in structured programming, which breaks down the complex algorithm process into several small tasks (blocks of code), makes the program structure clearer, improves the readability of the program, and is easy to maintain later and let others read your code.
In addition, to better reuse your code, you can abstract repetitive tasks into a single function.
The go language uses the keyword func to define a function, and the left curly brace cannot be another line, such as:
Func Listen hello () {listen//left brackets can not be heard from another line listen to listen to println ("Hello")}
When defining and applying functions in the Go language, here are some points to note:
Listen to a function without a predecessor declaration
No support for naming nested definitions, supporting anonymous nesting
The Listen function can only determine if it is nil and does not support other comparison operations
Listen to support multiple return values
Listen support named return value
Listen support return local variable pointer
Listen to support anonymous functions and closures
Func hear Hello () {Listen, listen, listen, hear, hear, listen to and/or open the parenthesis.}func listen to add (x, y listen int) listen to (sum listen int) {Listen to hear//name return value Listen listen sum listen = Listen x Listen + Listen y listen listen to return} Func Listen Vals (int,int) {Listen and listen listen//support multiple return values listen and hear return listen to 2,3}func listen to a () {}func Listen B () {}func listen to add (x, y listen int) listen (*int) {Listen and listen/ Support for returning local variable pointers listen listen to sum listen: = Listen x Listen + Listen y listen listen to listen to &sum}func listen to hear the main () {Listen to listen to hear println (a==b) listen to listen to listen to listen to listen to hear//can only judge whether nil, Do not support other comparison operations listen and listen to the Func listen to Hello () listen {Listen to listen to listen to hear//do not support named nested definition Listen Listen listen listen to listen to listen to println ("Hello")
Functions with the same signature (parameters and return values) are considered to be the same type of function, such as:
Func listen to Hello () listen {Listen and listen to the FMT. Println ("Hello")}func listen to say (f listen to Func ()) {Listen to listen to listen f ()}func listen to main () {Listen to listen f listen: = Listen to Hello listen listen to Say (f)}
Parameters:
The following points need to be noted in the go language for function parameters:
Listening does not support default parameters
Listen does not support named arguments
Listening to parameters as local variables of a function
Listening must pass the specified type and number of arguments in the order of signature
Listen to adjacent similar parameters can be combined
Listen to support indefinite long variable parameter, is essentially slice
Func Listen to test (x, y listen int, listen to s listen to string, listen to a bool) {Listen to listen to///adjacent to the same type parameters can be combined listen listen return}func listen to int listen to Parameter view as a local variable of a function listen and listen to the x listen: = Listen to listen to hear listen listen to listen to hear//no hear listen to hear listen to hear variables listen to listen to listen to the listen to hear the sound of listening to the left listen to side listen to Y listen to redeclared listen to this listen block listen to listen to hear +y}func hear the sum (Nums listen to ... int) Listen {listen//argument function listen to listen to total listen to listen to hear: = Listen to listen to the 0 listen to hear for listening _, listen to num listen: = Listen to the range listen to Nums listen to listen and listen to hear the total listen to listen to all + + hear and listen to listen to hear the FMT. Println (total)}func hear the main () {Listen to listen to//test ("s") listen to listen to listen listen to hear hear enough listen to listen to hear the arguments listen to the listen to listen to test listen to hear test ("s", FALSE) Listen and listen to nums listen: = Listen to []int{1, listen to 2, listen to 3} listen to the sum (Nums ...)}
Regardless of whether a pointer, reference, or other type parameter is passed, the value copy is passed, except that the target is the target object or the copy pointer.
Before a function call, the compiler allocates memory space for formal parameters and return values and copies the arguments to the parameter memory. Like what:
Func Listen test1 (x-*int) {Listen and listen to the FMT. Printf ("%p, Listen%v\n", &x listen, x)}func listen to main () {Listen to listen to listen to a listen: = Listen to the 0x100 listen to listen to hear P listen: = Listen to the &a listen and listen to the FMT. Printf ("%p, listen to%v\n", listen to &p, listen to P) listen and listen to Test1 (p)} output: 0xc42002c020, listen to 0xc42000a3200xc42002c030, listen to 0xc42000a320 from the structure, The listening arguments and parameters point to the same target, but the passed pointer is the assigned value.
If you have too many function parameters and return values, you can encapsulate them into a struct type, such as:
Type Listen serveroption listen to listen to struct{listen to listen to hear the addr listen to listen to hear the string hear the port listen int Listen listen to listen to listen to listen to the listen to listen to the listen to hear the time. Duration}func listen to Newoption () listen to listen to serveroption{listen to hear the return listen to hear &serveroption{listen listen to listen to listen to hear the addr: "127.0.0.1", listen to listen to hear listen to listen to hear the port : 8080, listen listen and listen to the path: "/var/www", listen and listen to listen to the timeout: listen to time. Second listen and listen to listen to hear listen to listen to hear}func (s listen to *serveroption) server () {Listen to listen to hear println ("Run listen to the server")}func listen to main () {Listen to listen to listen to: = Listen to the newoption () Listen to listen to S.port listen to listen to 80 listen to listen to hear s.server () listen to listen to listen to hear for{}
Variable parameter:
A parameter is essentially a slice (slice) that can receive only one or more parameters of the same type and must be placed at the end of the parameter list, such as:
Func listen to add (args hear ... int) Listen int Listen {Listen to total listen: = Listen to 0 Listen for listen _, listen to V listen: = Listen to the range listen to the args listen {Listen to listen to listen to the total listen to the + + listen to listen to the listen to hear the hear the return listen to the main () listen { Listen to the FMT. Println (ADD)}
If the parameter is a slice, can it be passed directly to a slice or an array?
Func listen to Test1 (s listen to String, listen to a listen ... int) {Listen and listen to the FMT. Printf ("%T, listen to%v\n", listen to A, listen to a) listen to listen to the//[]int, listen to [1 listen to 2 listen to 3 listen to listen to hear a listen to the 4]}{listen to: = Listen to [4]int{1,2,3,4} listen to listen to Test1 ("s", listen to a) listen to listen to listen to listen to listening to listen to Type Listen [4]int) Hear as listen to the type listen int listen in listen argument listen to listen to listen to listen to listen to listen to listen to listen to listen to listen to hear hear hear hear hear hear hear listen and listen heard listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, hear, hear, hear, hear, hear, hear, hear, Listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, Listen to the type listen int listen in listen to argument listen to listen to test1 listen to hear test1 ("s", listen to a[:] ...) Listen//Slice expand}
Since the argument is a slice, the parameter copies the slice itself, does not include the underlying array, so you can modify the original data, but you can copy the underlying data to prevent the original data from being modified, such as:
Func Listen test1 (a listen to ... int) {Listen to listen for listen I listen to: = Listen to a{listen to listen to listen to listen to listen to hear listen to listening to A[i] listen to + + listen to listen to the 100 listen to hear the main () listen to listen to a listen to listen to hear Listen to B listen: = Listen to make ([]int,0) listen to listen//Listen to copy (b,a[:]) Listen to hear listen to hear listen to listen to listen to hear/hear Test1 (b[:] ...) Listen and listen to Test1 (a[: ...) Listen to listen for listen I listen to: = Listen to a{listen to listen to hear and listen listen to the FMT. Println (A[i]) Hear and Hear}}
This article from "learned in the text, about the ceremony" blog, reproduced please contact the author!