[Golang] Function

2021-10-07 hit count image

Let's see how to define and use Function in Golang.

Outline

In this blog post, I will show you how to define the function and how to use the function in Golang. You can see the full source code of this blog post on the link below.

Define function

Let’s see how to define and use the function in Golang. To see how to define and use it, create the main.go file and modify it like the below.

package main

import "fmt"

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

func main() {
    c := Add(1, 2)
    fmt.Println(c)
}

You can define the function with the func keyword in Golang.

// func FUNCTION_NAME(PARAMETERS...) RETURN_TYPE {
//     CODE_BLOCK
// }
func Add(a int, b int) int {
    return a + b
}

If the paratmers are continuously same types, you can omit the type of the parameters like the below.

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

Public andPrivate

In Golang, there are no Public and Private keywords. But you can distinguish Public and Private by using the uppercase and lowercase of the name.

  • Function name starts with an uppercase letter: Public function
  • Function name starts with a lowercase letter: Private function

The Public function can be called by other packages, but the Private function can only be called by the package that defines it.

Multiple return values function

In Golang, you can define the function with multiple return values.

func Divide(a, b int) (int, bool) {
    if b == 0 {
        return 0, false
    }

    return a / b, true
}

func main() {
    c, success := Divide(10, 2)
    fmt.Println(c, success)
    d, success := Divide(10, 0)
    fmt.Println(d, success)
}

In Golang, when you use the short variable declaration(:=), if the variable name is already used, the compile error will occur.

func main() {
    c, success := Divide(10, 2)
    c, success := Divide(10, 0) // ERROR!
    fmt.Println(c, success)
}

The short variable declaration means that the new variable is created and assigned and we can’t use the duplicated name for creating a new value.

However, when you use the short variable declaration to get the value from the multiple return values function, if one of the variable names that are assigned from the multiple return values is different, the compile error won’t occur.

func main() {
    c, success := Divide(10, 2)
    fmt.Println(c, success)
    d, success := Divide(10, 0)
    fmt.Println(d, success)
}

Named return values

You can define the variables for the return values on the mutlipe return values function like the below.

func Divide(a, b int) (result int, success bool) {
    if b == 0 {
        result = 0
        success = false
        return
    }

    result = a / b
    success = true
    return
}

Completed

Done! we’ve seen how to define the function and how to use the function in Golang. Alos, we can learn how to define Public and Private functions in Golang with the function name.

Was my blog helpful? Please leave a comment at the bottom. it will be a great help to me!

Posts