go: Three dots/ellipses notation and variadic function; arrays vs slices

Go program example below explores arrays, slices and three dots notation. Arrays and slices are similar, arrays have static usage, slices are dynamic. Arrays are type values, fixed length sentences of items of the same type. Slices are reference types. A slice is a descriptor of an array segment. It consists of three parts: a pointer to the array, the length of the segment, and its capacity. Slice can be resized by append. If capacity would change, new slice is created with new reference descriptor!!! Example below shows, how to utilize variadic functions with slices. Variadic functions can have zero or many arguments of the same type. Variadic function can’t utilized arrays with three dots notation

Program below is also at https://play.golang.org/p/hoqXY5iRVBs

/* Three dots/ellipses notation and variadic function example
   (in go command ... means all files in the directory and subdirectories)
*/
package main

import (
	"fmt"
)

// show is a variadic function, it can have none or many arguments
func show(s ...string) {
	println()
	for i, val := range s {
		fmt.Printf("%d. %s\n", i, val)
	}
}
func main() {
	fmt.Println("Arrays are value types, fixed-length sequences of items of the same type.")
	fmt.Printf("Slices are reference types, can be resized by append.\n\n")

	seasons := []string{"Spring", "Summer", "Autumn", "Winter"} // slice
	choices := [2]string{"Good", "Bad"}
	days := [...]string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}

	dsun := make([]string, 7, 7) //slice

	// Let's populate dsun with Sunday first
	for i, d := range days {
		if i == 6 {
			dsun[0] = d
		} else {
			dsun[i+1] = d
		}
	}

	fmt.Println("Length of slice seasons is", len(seasons), ", capacity is ", cap(seasons))
	fmt.Println("Length of slice dsun is", len(dsun), ", capacity is ", cap(dsun))
	fmt.Println("Length of array days is", len(days))
	fmt.Println("Length of array choices is", len(choices))

	show("a", "b")
	show(seasons...)
	// show(days...) //cannot use days (type [7]string) as type []string in argument to show
	// show(choices...) //cannot use choices (type [2]string) as type []string in argument to show
	show(dsun...)
	show("The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog")
}
This entry was posted in golang, workday. Bookmark the permalink.

Leave a Reply