Functions in the Go language learning 6:go language

Source: Internet
Author: User
Tags define function
This is a creation in Article, where the information may have evolved or changed.

A complete function definition form in the Go language is as follows:

Func (t type) funcname (a int) (b,c int) {return)

    • 1.func is the keyword that defines the function
    • 2. (t type): This part is called the receiver section, which indicates that the function is used for a particular type
    • 3.funcname: The name of the function
    • 4. (a int): parameters accepted by the function
    • 5. (b,c int): function return value, go language function is can return multiple values at the same time
    • 6.{return: The body part of the function
There is no need to define function prototypes in the go language, because the go language searches for all files at compile time, so function prototypes do not exist in go. The variables of function in Go are also used in the same way as the C language.
Here's a look at the return of multiple values of the Go function:See an example of a multivalued return:
Func getindexandvalues (shu []int,index  int) (int,int) {x:=index        Y:=shu[index]         return x, y} The function itself does not mean anything, But we can see that it also returns two values X and Ygo functions support named return values, and the named return value can be used directly in the function, so the above function can be rewritten as: Func getindexandvalues (shu []int,index  int) (x int,y int) {X=index        Y=shu[index]         return x, y} if the function returns without parameters, the function will return the current value of the named parameter by default: Func Getindexandvalues (Shu []int,index  int) (x int,y int) {X=index        Y=shu[index]         return} This function, like the function above, returns X, The value of Y the Go function also has a very important function that is delay:


Why do we need to delay the function? Recall a situation in which we open a file read and write in C or another language:
void file ()
{
F=open ("Sagag.txt"),                if (f is empty) {close (f); Return;}......if (read error) {close (f); Return;}......if (read error) {close (f); return;} ...
}
In short, we will encounter a lot of abnormal situation, need close file, so that caused a lot of duplicated code and operation, the go function of the delay function is to solve the problem. Go uses the keyword defer to specify the delay function, go specifies that the delay function is only called when the function that called it exits, so the above code can be rewritten as:
Func file () {    File.Open ("Asdag.txt")    defer file.close () ... .... do   something   return} (".).."
Delay functions can delay not only a single statement, but also a list
For I: = 0; I < 5; i++ {       defer FMT. Printf ("%d", I)}
The actual output of the above function is: 4 3 2 1 0 because the deferred list is in the same advanced post-out mode as the stack. The function values of the Go language support arguments:Defines the form of a function that can accept arguments: Func myfunc (arg ... int) {}, the types of these parameters are all int, in the function body, the variable arg is an int type of slice, and if you do not specify the type of the parameter, the default is an empty interface interface{}.

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.