This is a creation in Article, where the information may have evolved or changed.
First, the signature
1. Go supports variable and assignment of function type. For example:
Package Mainimport "FMT" var PR = fmt. Printlnvar f = func () { PR ("Hello")}var G func () Func main () { g = f g ()}
Second, the parameters
1. Using Godoc fmt Println, you can see that its signature is:
Func Println (A ... interface{}) (n int,err error)
This one... The parameter A that represents println can accept multiple values, and this interface{} indicates that a is an empty interface type, that is, you can accept values of almost all types.
Third, return statement
1, in fact, for each go program, we are taking advantage of its side effects, because the signature of the main function has no side effects. This differs from the C program. The main function of the C program will return
An integer value indicates the error of the program. In go, this requires the Exit function of the OS package:
Func Exit (code int)
Four, function call
Five, closed package
1, although we can not directly declare another function in a function, but can be declared in a function of a function type of variable. The function at this time is called closure (closure). For example:
Package Main
Import "Main"
Func Main () {
Add: = func (base int) func (int) int {
return func (n int) int {
Return base + N
}
}
ADD5: = Add (5)
Fmt. Println (ADD5 (10))
}
Six, after the pressure
1. Defer statement register a function after pressing. When the function returned by defer, call defer registered function. For example
Func f () {
ERR: = Lock (L)
Defer unlock (l)
If Err {return}
Go ()
}
Defer ensure that the unlock will be executed at the end of f () execution.
The defer expression must be a function or method call. Each time the defer statement executes, the function 's arguments are evaluated but the function is not called immediately . Functions that are registered after the post-press statement are executed in the order of LIFO (last in, first out), until the function where the defer is returned.