#1 India's Top IT Training Institute

×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
Functions

Functions in Go – Multiple Returns, Variadic, and Defer Tutorial

Basic Function Syntax

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

Multiple Return Values

func divide(a, b int) (int, error) {
    if b == 0 {
        return 0, errors.New("cannot divide by zero")
    }
    return a / b, nil
}

result, err := divide(10, 2)

Variadic Functions

Variadic functions accept a variable number of arguments using ....

func sum(nums ...int) int {
    total := 0
    for _, n := range nums {
        total += n
    }
    return total
}

sum(1, 2, 3, 4) // 10

The defer Keyword

defer schedules a function call to run just before the surrounding function returns — commonly used for cleanup tasks like closing files.

func readFile() {
    file, _ := os.Open("data.txt")
    defer file.Close()
    // read file contents
}

Ready to master real-world Go skills?

Learn Go hands-on with mentor-led, live sessions.

Explore Course