Continuing with variadic functions in Go

Follow the latest of Whizdumb’s journey here.

Now that we know, about variadic functions Lets explore them a little more.

The most common variadic function we have been using all along is fmt.Println().

We can use that function to print just a String.

package main
import "fmt"

func main() {       
    fmt.Println("Hi this is a sample string")  
}


or we can print a string and a float

package main
import "fmt"

func main() {       
    fmt.Println("Hi this is a sample string", 4.5)  
}

or we can print a string, a float and multiple int values

package main
import "fmt"

func main() {       
    fmt.Println("Hi this is a sample string", 4.5, 4,5,6,67,23)  
}

Notice the different types here along with different number of arguments. How does Println function does that?

From the documentation, the println function looks like

func Println(a ...interface{}) (n int, err error)

So it can accept multiple parameters of type interface{}, we will explore interface{} later, but for now lets take it that it allows Println to account for all basic type

It also has 2 return types one int and one error, Here is how they work

package main
import "fmt"
func main() {
    x,y := fmt.Println("Hello, world")
    fmt.Println(x,y)
}

If all goes well the above code prints

Hello, world
13 

Signifying that 13 bytes were written and no error was encountered.

Lets explore another function, lets try to append value of one slice to another slice. We will use builtin append function. The signature of append function looks like

func append(slice []Type, elems ...Type) []Type

It accepts a slice as a first argument and thereafter variable number of arguments. But how can we use it to append one slice to another. We can explode the second slice to append it behind the first. Here is how to do it

package main
import "fmt"
func main() {
     x := []int{
    48,96,86,68,
    57,82,63,70,
    37,34,83,27,
    19,97, 9,17,
}   

  y := []int{
    48,96,86,68,
    57,82,63,70,
    37,34,83,27,
    19,97, 9,17,
}   

   z:= append(x,y...)
   fmt.Println(z)

}

Notice the line z:= append(x,y…) . The … behind slice y are very important since they tell the compiler to actually explode the slice and pass in each value as a parameter to the function. The output from above code is

[48 96 86 68 57 82 63 70 37 34 83 27 19 97 9 17 48 96 86 68 57 82 63 70 37 34 83 27 19 97 9 17]

Later!

~~ Whizdumb ~~

Passing variable number of arguments in a function

Follow the latest of Whizdumb’s journey here.

Continuing with our learning of functions in go, lets now explore how a function can accept variable number of arguments in a function.

Lets modify calculateMinimumAndMaximum(x []int) (int,int) from previous post to accept a variable number of arguments rather than an array or slice.

Lets update the function signature and definition first.

func calculateMinimumAndMaximum(x ...int) (min int, maximum int) {
    minimum := -1
    max := 0    
    for _, v := range x {
        if minimum == -1 || v < minimum {
          minimum = v
        }
        if max < v {
            max = v
        }
    }
    return minimum, max 
}


Here we have defined a function which accepts a variable number of arguments of type int. The functions are called Variadic functions

Now we can call this function with a variable number of arguments, all of which should be of type int.

Here is how the whole thing looks like.

package main

import "fmt"

func main() {      
    minimum, maximum := calculateMinimumAndMaximum(48,96,86,68,
    57,82,63,70,
    37,34,83,27,
    19,97, 9,17,)
    fmt.Println("Minimum value is", minimum)
    fmt.Println("Maximum value is", maximum)   
}

func calculateMinimumAndMaximum(x ...int) (min int, maximum int) {
    minimum := -1
    max := 0    
    for _, v := range x {
        
        if minimum == -1 || v < minimum {
        
          minimum = v
        }
        
        if max < v {
         
            max = v
        }
    }
    return minimum, max 
}

~~ Whizdumb ~~