Function of Go language programming (eight)

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

Functions form the logical structure of code execution. In the Go language, the basic composition of a function is: The keyword func, the function name, the argument list, the return value, the function body, and the return statement.

function definition

Use one of the simplest addition functions for detailed instructions:

Package Main
Import(
  "errors"
  "FMT"
)
func    add ( a  int  Span style= "COLOR: #000000",    b  int  )     ( ret  int  Span style= "COLOR: #000000",    err  Span style= "COLOR: #000080" >error  )    Span style= "COLOR: #000000" >{ 
&NBSP;&NBSP;&NBSP;&NBSP;  if    a  <    0    Span style= "COLOR: #000000" >| |    b  <    0    Span style= "COLOR: #000000" >{ 
 err=errors. New("shouldbenon-negativenumbers!" )
  return
    }
  returnA +b,nil// Support for multiple return values
}
If several adjacent parameter types are the same in the parameter list, such as a and B in the example above, you can omit the type declaration of the preceding variable in the argument list, as follows:

FuncAdd (a,bint) (retint ,  Errerror) {
    //
}
If more than one return value in the return value list is of the same type, it can be combined in the same way.

If a function has only one return value, it can also be written like this:

FuncAdd (a,bint)int {
    //
}
function Call

The function call is very simple, as long as the package containing the function is imported beforehand, the function can be called directly as follows:

Import"MyMath"    
C: =mymath. Add(1,2)
Note that a function that starts with a lowercase letter is visible only in this package, and a function that begins with a capital letter can be used by another package. This rule also applies to the visibility of types and variables.

Indeterminate parameters

An indeterminate parameter is a variable number of arguments passed in. To do this, you first need to define the function to accept the indeterminate parameter type:

Funcmyfunc (args ... int ){
  for_,arg:= range  args{
  fmt. Println(arg) 
    }
}
The meaning of this code is that the function MyFunc () accepts an indefinite number of arguments whose types are all int, so it can be called in the following way:

MyFunc(2,3,4)
MyFunc(1,3,7   ,  )
shaped like ... type format can only exist as a function parameter type, and must be the last parameter.

The previous example constrains the indeterminate parameter type to int, and if you want to pass any type, you can specify a type of interface{}. Here is the function prototype for fmt.printf () in the Go Language standard library:

FuncPrintf (formatstring,args ... Interface {}){
    //
}
The following is a complete example:

  varg1
Package Main
Import(
  "FMT"
)
Funcmyprintf (args ... Interface {}){
  for_,arg:=Range   args{
  switcharg. ( type ){
   case int:
 fmt. Println(arg,"is anint    Value. " )
   case string:
 fmt. Println(arg,"isastring   Value. " )
   case Int64:
 fmt. Println(arg,"is anint64    Value. " )
  default:
 fmt. Println(arg,"is anunknown   type. " )
        }
    }
}
FuncMain (){
  var v1 int=1
  var v2 Int64=234
  varv3 string="Hello" 
  var v4 float32=1.234
 myprintf(v1,v2,v3,v4 )
}

Multiple return values

Multiple return value functions are supported in the go language.

If the caller calls a method with multiple return values, but does not want to care about one of the return values, you can simply use an underscore to skip the return value.

Anonymous functions and closures

An anonymous function is a function implementation that does not require the definition of a function name.



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.