Go Language Learning (quad)-functions

Source: Internet
Author: User
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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.