Skip to main content
Module 3 · Lesson 3 of 513/50 in the course~10 min
Module lessons (3/5)

Variadic functions

Lesson preview5 hands-on exercises readyCourse Go · Module 3 — Functions

func sum(nums ...int) int, spread of slice with slice..., examples from stdlib.

What you'll learn in this lesson

  • Declare variadic functions with ...T as the last parameter and read their arguments as slice []T.
  • Expand an existing slice during the call using the spread operator slice....
  • Avoid side changes when a slice passed with slice... is reused internally.

Overview

A variadic function takes zero, one, or infinite arguments of the same type. This is the same mechanism by which fmt.Println accepts any number of values or append concatenates elements.

Only the last parameter of the signature can be variadic. Inside the body of the function it takes on exactly the type []T:

Sections covered in this lesson

  1. 1Declaration with ...T
  2. 2Fixed + variadic and expansion parameters slice...
  3. 3Sharing the slice with slice...
  4. 4Passage ...any and printing flexibility
  5. 5Guided practice
  6. 6Check for understanding
Pro Lesson · Module 3 — Functions

Unlock "Variadic functions" and the full Course Go curriculum

`func sum(nums ...int) int`, spread of slice with `slice...`, examples from stdlib.

Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.

  • Instant access to all advanced modules in Course Go plus 17 more courses
  • Hands-on browser exercises with automated test suites and solutions
  • 14-day money-back guarantee processed via Paddle.com

14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.