5. Go function

Source: Internet
Author: User
Tags define function



"Define function"



Directly on the chestnut, go language defines the function:


func add(a int, b int) int {
    return a + b
}


At a glance, not too accustomed to the name of the go language, type why write to the back?



"Multiple Return Values"



The Go function can also return multiple values:


func add(a int, b int)(int , int){
    return a, a + b
}


"Named Return value"


func add(a int, b int) (c int) {
    c = a + b
    return
}


According to the internet, the named return value is, as in the above code. The function automatically defines C, and it automatically returns C.



"Multiple Identical types"



If the function parameter has more than one type, then one is written, and the named return value applies.


func add(a, b int) (c int) {
    c = a + b
    return
}


"White space character"



The function returns multiple parameters, and we only need to use one, and the other parameters do not need to use the whitespace character ' _ ', an underscore.


package main 

import (
    "fmt"
)

func add(a, b int) (c, d int) {
    c = a + b
    d = a * b
    return
}

func main() {
    c, _ := add(3, 4)
    fmt.Printf("c = %v\n", c)
}


5. Go function


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.