golang function (1)

Go inside, there are three types of functions:

1. Ordinary functions with names

2. anonymous function or lambda function

3. Methods

 

Reload function (function overloading) can be prepared by means of a plurality of functions with the same name, as long as they have different shape participation / or different return values, in which Go function overloading is not allowed to. This will result in a compile error:

funcName redeclared in this book, previous declaration at lineno

The main reason Go language does not support this feature is a function of the need for extra heavy-duty type matching affect performance ; not override means simply a function of scheduling. So you need to use different names to different functions, we usually named according to function characteristic function

 

Function can also be used to affirm the way, as a function of the type, like:

type binOp func(int, int) int // 类型

 

And passed by value is passed by reference

Go default passed by value to pass parameters, that is, pass a copy of the parameters. May make changes to the copy of the value of the variables used in the process will not affect the original variables

If you want the function can directly modify the value of the parameter, rather than a copy of the parameters of the operation, you need to address arguments (a pointer or reference) (added before the variable name & symbols, such as & variable) passed to the function, which is by reference transfer, we can modify the value of the value at the address pointed to by the pointer value. (Translator's Note: the variable is a pointer type, and has its own address value, the address pointers to the normal value of a variable)

I think it is passed by reference are passed by value, but the value is referenced memory address.

In almost any case, passing a pointer (a 32-bit or 64-bit value) consumed far less than the transfer copy.

In the function call, as a slice (Slice), dictionary (Map), the interface (interface), the channel (channel) is the default reference type such use the reference transmission (even if not explicitly stated pointer).

Guess you like

Origin www.cnblogs.com/dream-again/p/11495888.html