Functions in Go

Go: Functions

Functions are very important part of Golang and one of Go's unusual features is that functions and methods can return multiple values as well.

Function example in Go

here is an example of functions

    package main
    import "fmt"

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

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

    func main() {
        res := plus(1, 2)
        fmt.Println("1+2 =", res)
        res = plusPlus(1, 2, 3)
        fmt.Println("1+2+3 =", res)
    }

Multiple Return Values Functions

    package main
    import "fmt"
    The (int, int) in this function signature shows that the function returns 2 ints.

    func sumAndMultiply() (x int, y int) {
        return x+y, x*y
    }
    func main() {
        s, m := vals()
        fmt.Println(s)
        fmt.Println(m)
    
        //If you only want a subset of the returned values, use the blank identifier _.
        _, c := vals()
        fmt.Println(c)
    }

To Understand functions in details read this article Functions in Go

Next Article