September 2017
Intermediate to advanced
466 pages
9h 33m
English
This subsection will perform a practical operation, which is converting an array into a map without knowing the size of array in advance. The Go code of array2map.go can be divided into three main parts. The first part is the standard Go code that includes the required packages and the beginning of the main() function:
package main
import (
"fmt"
"strconv"
)
func main() {
The second part, which implements the core functionality, is as follows:
anArray := [4]int{1, -2, 14, 0}
aMap := make(map[string]int)
length := len(anArray)
for i := 0; i < length; i++ {
fmt.Printf("%s ", strconv.Itoa(i))
aMap[strconv.Itoa(i)] = anArray[i]
}
You first define the array variable and the map variable you will use. The for loop ...
Read now
Unlock full access