Variadic Functions in Go

GolangGolangBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In Go, a function that can take a variable number of arguments is called a variadic function. This lab will test your understanding of how to use variadic functions in Go.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL go(("`Go`")) -.-> go/FunctionsandControlFlowGroup(["`Functions and Control Flow`"]) go/FunctionsandControlFlowGroup -.-> go/functions("`Functions`") subgraph Lab Skills go/functions -.-> lab-15528{{"`Variadic Functions in Go`"}} end

Variadic Functions

In this lab, you need to implement a function named max that takes an arbitrary number of integers as arguments and returns the maximum value.

  • The function max should take an arbitrary number of integers as arguments.
  • The function max should return the maximum value of the integers passed as arguments.
$ go run variadic-functions.go
[1 2] 3
[1 2 3] 6
[1 2 3 4] 10

## Another key aspect of functions in Go is their ability
## to form closures, which we'll look at next.

There is the full code below:

// [_Variadic functions_](https://en.wikipedia.org/wiki/Variadic_function)
// can be called with any number of trailing arguments.
// For example, `fmt.Println` is a common variadic
// function.

package main

import "fmt"

// Here's a function that will take an arbitrary number
// of `int`s as arguments.
func sum(nums ...int) {
	fmt.Print(nums, " ")
	total := 0
	// Within the function, the type of `nums` is
	// equivalent to `[]int`. We can call `len(nums)`,
	// iterate over it with `range`, etc.
	for _, num := range nums {
		total += num
	}
	fmt.Println(total)
}

func main() {

	// Variadic functions can be called in the usual way
	// with individual arguments.
	sum(1, 2)
	sum(1, 2, 3)

	// If you already have multiple args in a slice,
	// apply them to a variadic function using
	// `func(slice...)` like this.
	nums := []int{1, 2, 3, 4}
	sum(nums...)
}

Summary

In this lab, you learned how to use variadic functions in Go. You implemented a function named max that takes an arbitrary number of integers as arguments and returns the maximum value.

Other Golang Tutorials you may like