Returning a Single Value
Returning a single value from a function is a common practice in Go. One key aspect is ensuring that the returned value matches the type declared in the function’s signature. Consider the following example where the function is declared to return a string, but it mistakenly returns an integer:Returning Multiple Values
Go provides the flexibility to return multiple values from a function. When declaring such a function, you enclose the return types in parentheses. The following example demonstrates a function,operation, that accepts two integers and returns both their sum and difference:
Named Return Parameters
By naming the return parameters in the function signature, Go allows you to simplify the function’s return statement. Here’s how theoperation function looks when using named return parameters:
When you name the return parameters, you can use an empty
return statement to return the current values of those parameters. This approach can make your code clearer and easier to maintain.Variadic Functions
Variadic functions accept a variable number of arguments. In Go, you declare a variadic parameter by prefixing its type with an ellipsis (...). This feature is useful for functions where the number of inputs may vary.

numbers behaves like a slice containing all the passed integers. The following complete example demonstrates how sumNumbers calculates the sum of all provided integers:
- 0 (when no arguments are provided)
- 10 (with a single argument)
- 30 (10 + 20)
- 150 (10 + 20 + 30 + 40 + 50)
Variadic Functions with Multiple Parameters
A variadic function can accept additional parameters, but the variadic parameter must always be the last in the function signature. The example below shows a function that takes a mandatory string parameter followed by a variadic string parameter:student parameter, and the remaining arguments are collected in the subjects slice.
Using the Blank Identifier
In cases where a function returns multiple values but not all are needed, the blank identifier (_) is used to ignore the unneeded values. Consider the following example where the function f returns two integers:
_ effectively acts as a placeholder to ignore any return value you do not need.
That concludes our comprehensive overview of return types in Go, including single returns, multiple returns, named return parameters, variadic functions, and the use of the blank identifier. These concepts are key to writing flexible and maintainable Go programs. Happy coding!