Prev Next

Golang / GoLang Basics Interview Questions

How are functions defined in Go? What are variadic functions and named return values?

Functions are first-class citizens in Go — they can be assigned to variables, passed as arguments, and returned from other functions. Go functions support multiple return values (the primary mechanism for error handling), named returns, and variadic parameters.

// Basic function with multiple return values
func divide(a, b float64) (float64, error) {
    if b == 0 { return 0, errors.New("division by zero") }
    return a / b, nil
}

// Named return values — document what is returned
// Use sparingly; best for short functions only
func minMax(nums []int) (min, max int) {
    min, max = nums[0], nums[0]
    for _, n := range nums[1:] {
        if n < min { min = n }
        if n > max { max = n }
    }
    return  // naked return — returns named values min and max
}

// Variadic function — accepts 0 or more arguments of the given type
func sum(nums ...int) int {
    total := 0
    for _, n := range nums { total += n }
    return total
}
sum(1, 2, 3)        // 6
s := []int{4, 5, 6}
sum(s...)           // 15 — spread a slice with ...

// Function as a value (first-class)
double := func(n int) int { return n * 2 }
fmt.Println(double(7))  // 14

// Higher-order function
func apply(nums []int, f func(int) int) []int {
    result := make([]int, len(nums))
    for i, v := range nums { result[i] = f(v) }
    return result
}
fmt.Println(apply([]int{1, 2, 3}, double))  // [2 4 6]

What syntax spreads a slice into a variadic function argument?
What is a 'naked return' in Go?

More Related questions...

What is Go and why was it created at Google? What are the key characteristics that make Go different from other popular languages? What are packages in Go and what is special about the 'main' package? What are the different ways to declare variables in Go? What are the fundamental data types in Go? How do constants and iota work in Go? How are functions defined in Go? What are variadic functions and named return values? How do if, for, and switch statements work in Go? What is the difference between arrays and slices in Go? How do maps work in Go? What are the key operations and pitfalls? How do structs work in Go and how do you attach methods to them? How do interfaces work in Go? How do you use type assertions and type switches? What is the empty interface (any / interface{}) and when should you use it? How do pointers work in Go and how are they safer than C pointers? How does Go handle errors, and what is the difference between %v and %w in fmt.Errorf? What are goroutines and how do you use sync.WaitGroup to wait for them? What are channels in Go and what is the difference between buffered and unbuffered? How do defer, panic, and recover work together in Go? What are closures in Go and what is the loop variable capture gotcha? What is the init() function and when does it run? What is the Go module system? What do go.mod and go.sum contain? What is a data race in Go and how do you detect one? What is the fmt.Stringer interface and how does it control how a type is printed? What is the difference between a type definition and a type alias in Go? How does Go handle strings, runes, and bytes? Why is len(s) not the character count? What is a goroutine leak and what is the idiomatic way to prevent one? What is sync.Mutex and when do you use sync.RWMutex instead? How does struct embedding promote methods in Go, and how does it differ from inheritance? How does append() work internally in Go? When does it allocate new memory? What is context.Context and why is it the first parameter in so many Go functions? What is the 'typed nil' trap in Go and why does 'if err != nil' sometimes fail? What are the most important formatting verbs in Go's fmt package?
Show more question and Answers...


Comments & Discussions