Golang Basic--func function

Source: Internet
Author: User

Functional function

  • The GO function does not support nesting, overloading, and default parameters
  • The following features are supported:

    No need to declare prototypes, variable length length arguments, multiple return values, named return value parameters, anonymous functions, closures

  • The
  • Definition function uses the keyword func, and the left brace cannot be another line

      package Mainimport ("FMT") func main () {A, B: = 1, 2 D (A, B)//pass variable length variable, cannot modify slice index, cannot change slice value at all s1: = []int{1, 2, 3} D1 (S1)//pass slice as parameter, modify index value can achieve effect of modifying original slice x: =  1 D2 (&X)//Pass x memory address}//Parameter return value func a (a int, b string) int {return a}//multiple return values Func B () (A, B, c int) {//No parameters, A, b, C = 1, 2, 3//Because the return value already has a,b,c occupy the inner layer, so directly after the assignment return a, B, c//can omit a,b,c}//variable length arguments, that is, the int parameter can be multiple, passed in is a slice type func C (b String, a ... int) {FMT. Println (a)//output [1,2,3,4,5]}/variable-length parameter pass, passing in variables func D (s ... int) {FMT. Println (s) s[0] = 3 S[1] = 4 FMT. Println (s)}//passes slice as a parameter, copying slice memory address func D1 (s []int) {s[0] = 4 S[1] = 5 FMT. Println (s)}//pointer type passed, can achieve the purpose of modifying variable value func D2 (a *int) {*a = 2//Memory a value becomes 2 fmt. Println (*a)}/* output D----> [1 2]//slice [3 4]//slice 1 2//intd1----> [4 5 3]d2-- --2//parameter is pointer can modify variable value */ 
  • Functions can also be used as a type

    package mainimport "fmt"func main() {    a := A //函数作为类型使用    a()}func A() {    fmt.Println("Func A")}//指针类型传递,可以达到修改变量值的目的/*输出a()---->    Func A*/

    The function name can be interpreted as the number of the memory address, which can be used to assign a value operation.

  • anonymous function, different with the python language has a keyword lamada , go anonymous function explicit more straightforward, no function name, only need the Func keyword can
    Simply build an anonymous function, as follows

    // 匿名函数package mainimport "fmt"func main() {    a := func() { //没有函数名,直接进行赋值给变量        fmt.Println("Func A")    }    a() //调用匿名函数}
  • Closure of a function: Also called a nested function, the returned type is a function, the function is assigned to the variable, re-passed to the parameter execution

    package mainimport "fmt"func main() {    f := closure(10)    fmt.Println(f(3))    fmt.Println(f(4))}func closure(x int) func(int) int { // 函数作为返回类型    return func(y int) int {        return x + y    }}/*输出f(3)--->    13f(4)--->    14*/
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.