Variadic Functions
Accepting a variable number of arguments
Variadic Functions is a free Go Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is a Variadic Function?
A variadic function accepts zero or more arguments of a specified type, collected into a slice:
func sum(nums ...int) int {
total := 0
for _, n := range nums {
total += n
}
return total
}
fmt.Println(sum(1, 2, 3, 4)) // 10The ...T Syntax
Use ...T as the last parameter type. Inside the function, it is a []T:
- Can be called with 0 or more arguments
- The variadic parameter must be the last one
- Only one variadic parameter per function
Spreading a Slice
Pass an existing slice to a variadic function using the spread operator ...:
nums := []int{1, 2, 3, 4, 5}
total := sum(nums...) // spread the slice
fmt.Println(total) // 15fmt.Println is Variadic
Many standard library functions are variadic. fmt.Println has signature func Println(a ...interface{}) (n int, err error) — it accepts any number of values of any type.
append is Variadic
The built-in append is variadic:
a := []int{1, 2, 3}
b := []int{4, 5, 6}
a = append(a, b...) // append all of b into a
fmt.Println(a) // [1 2 3 4 5 6]Variadic with Other Parameters
Variadic parameters come last. Mix fixed and variadic:
func logf(level string, msg string, args ...interface{}) {
fmt.Printf("[%s] "+msg+"\n", append([]interface{}{level}, args...)...)
}Empty Variadic Call
A variadic function can be called with no arguments — the slice will be nil (or empty):
fmt.Println(sum()) // 0 — nil slice, loop runs 0 timesVariadic vs Slice Parameter
When to use variadic vs a slice parameter:
- Variadic: when callers provide individual values inline (e.g.,
sum(1,2,3)) - Slice: when callers always have a pre-built collection
- Variadic is more ergonomic at call sites; both work equally well internally
Passing Variadic to Another Variadic
Forward variadic arguments using spread:
func logError(args ...interface{}) {
log.Println(args...) // forward to log.Println
}Building Option Patterns
Variadic ...Option is a popular pattern for optional configuration (functional options):
type Option func(*Server)
func NewServer(addr string, opts ...Option) *Server {
s := &Server{addr: addr}
for _, o := range opts {
o(s)
}
return s
}Quick Check
How do you pass a slice to a variadic function?
Recap: Variadic Functions
Variadic functions make call sites clean:
...Tcollects trailing arguments into a[]Tinside the function- Call with individual values or spread a slice with
slice... - Built-ins like
appendandfmt.Printlnare variadic - Only one variadic param allowed, and it must be last
- Use functional options pattern for expressive API design
Next: defer, anonymous functions, and closures.
Frequently asked questions
Is the “Variadic Functions” lesson free?
Yes — the full text of “Variadic Functions” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.
What will I learn in “Variadic Functions”?
Accepting a variable number of arguments You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Go Academy?
No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Variadic Functions” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Go Academy lesson?
Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.