Skip to content
Justice Tech School

Variadic Functions

Use ...T to accept any number of arguments as a slice. Spread a slice with ....

Example
package main

import "fmt"

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

func main() {
	fmt.Println(sum(), sum(1, 2, 3))
	n := []int{4, 5}
	fmt.Println(sum(n...))
}

Result

0 6
9

Exercise

Variadic syntax.

func f(n int)

Quiz Test

What type is n inside?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.