Golang function Notes

Source: Internet
Author: User

First, the concept

function allows us to package a sequence of statements into a single unit, which can then be called multiple times from other parts of the program. The mechanism of the function allows us to break a big job down into small tasks that allow different programmers to perform independently at different times and places. A function hides its implementation details from the user at the same time. Because of these factors, functions are a vital part of any programming language.

Ii. Declaration and use of functions

Package Mainimport (    "fmt") Func main () {    say ("  Hello"string"{    fmt. Println ("Say:", info)}

Three, multiple return value functions

Package Mainimport (    "fmt") Func main () {    ID, name:Get ()    Get() (intstring) {    return1  "frod"}

Iv. Anonymous functions

Package Mainimport ("FMT") Func main () {//passing an anonymous functionRes: = SUM (func () (int,int) {return 1,2}) fmt. Println (RES)}//anonymous functions as arguments to functionsFunc sum (f func () (int,int))int{A, B:=f ()returnA +B}

Five, anonymous function-closure function

Package Mainimport (    "fmt") Func main () {    //  sum () Inside is a closure function, the closure function is immediately executed    res: = SUM (func () (intintreturn12  } ())    intint  {    return A + B}

Six, anonymous function-nested, meaning is not particularly large, just for basic learning

Package Mainimport (    "fmt") Func main () {    res:int  {        A, B:= func () (intintreturn12  } ()         return A + b    } ()    FMT. Println (RES)}

Vii. functions-Variable parameters

 package mainimport (  " fmt  "  ) func main () {FMT. Println (SUM ( 1 , 2 , 3 , 4 , 5 , 6   int ) int   { var  sum  int  for  V: = range vals {sum  +=< Span style= "COLOR: #000000" > v}  return   sum} 

Golang function Note

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.