Go Functions Return Values

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!


A function can return one or more values to the caller:

func AddAndMultiply(a, b int) (int, int) {
    return a+b, a*b

The second return value can also be the error var :

import errors

func Divide(dividend, divisor int) (int, error) {
    if divisor == 0 {
        return 0, errors.New("Division by zero forbidden")
    return dividend / divisor, nil

Two important things must be noted:

  • The parenthesis may be omitted for a single return value.
  • Each return statement must provide a value for all declared return values.

Got any Go Question?