Skip to main content

Command Palette

Search for a command to run...

variadic function

Published
1 min readView as Markdown
S
DevRel at StackGen | Formerly at Deepfence ,Tenable , Accurics | AWS Community Builder also Docker Community Award Winner at Dockercon2020 | CyberSecurity Innovator of Year 2023 award by Bsides Bangalore | Docker/HashiCorp Meetup Organiser Bangalore & Co-Author of Learn Lightweight Kubernetes with k3s (2019) , Packt Publication & also run Non Profit CloudNativeFolks / CloudSecCorner Community To Empower Free Education reach out me twitterhttps://twitter.com/sangamtwts or just follow on GitHub -> https://github.com/sangam14 for Valuable Resources

What is variadic function?

variadic function is simple function which accepts many arguments.and those arguments store parameter as slicen type.

func f(elem ...Type)

A typical syntax of a variadic function looks like above. ... operator called as pack operator instructs go to store all arguments of type Type in elem parameter. With this syntax, go creates elem variable of the type []Type which is a slice. Hence, all arguments passed to this function is stored in a elem slice.

package main

import "fmt"

func getMultiples(factor int, args ...int) []int {
    multiples := make([]int, len(args))

    for index, val := range args {
        multiples[index] = val * factor
    }

    return multiples
}

func main() {
    s := []int{10, 20, 30}

    // get multiples of 2, pass parameters from slice using `unpack` operator
    mult1 := getMultiples(2, s...)

    // get multiples of 3
    mult2 := getMultiples(3, 1, 2, 3, 4)

    fmt.Println(mult1)
    fmt.Println(mult2)
}

try out -Go Playground

in the above program func getMultiples(factor int, args ...int) []int { this is variadic fuction with two types variable in which one is factor with data type int and another is ( unpack operator ) which means n number of parameters.

mult1 and mult2 is two shore declartion variable which execute getmultiples variadic function and first argument is factor

1 views

More from this blog

C

CloudNativeFolks Community

140 posts

CloudNativeFolks is non profit community that empower and educate about cloud native technology !