by DonOfDen
Posted on 20 Sep 2019
Tags: go golang map array slice range map
0
Array Type Example:
// Aray of String
var myStringArray [2]string
myStringArray[0] = "Hey, "
myStringArray[1] = "You!"
// Integer Array
var myIntegerArray [2]int
myIntegerArray[0] = 1
myIntegerArray[1] = 2
// Time Array
var dates [1]time.Time
// Assign values
dates[0] = time.Unix(1257894000,0)
Fun Stuff
var counters [3]int
counters[0]++
If you know in advance what values an array should hold, you can initialize the array with those values using an array literal.
package main
import "fmt"
func main() {
intArray := [...]int{10, 20, 30, 40, 50}
fmt.Println("\n---------------Example 1--------------------\n")
for i := 0; i < len(intArray); i++ {
fmt.Println(intArray[i])
}
fmt.Println("\n---------------Example 2--------------------\n")
for index, element := range intArray {
fmt.Println(index, "=>", element)
}
fmt.Println("\n---------------Example 3--------------------\n")
for _, value := range intArray {
fmt.Println(value)
}
j := 0
fmt.Println("\n---------------Example 4--------------------\n")
for range intArray {
fmt.Println(intArray[j])
j++
}
}
[start:end]
extract operatorpackage main
import "fmt"
func main() {
s := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
fmt.Println("s", s)
fmt.Println("s[:]", s[:])
fmt.Println("s[2:]", s[2:])
fmt.Println("s[:4]", s[:4])
fmt.Println("s[2:4]", s[2:4])
}
Every slice is built on top of an underlying array. It’s the underlying array that actually holds the slice’s data; the slice is merely a view into some (or all) of the array’s elements.
Change the underlying array, change the slice
We can use any type in map index, Whereas arrays and slices can only use integers as indexes, a map can use any type for a keys (As long as values of that type can be compared using ==).
The values & Keys all have to be of same type. But keys and values dont have to be the same type.
Slice & Map use Make function.
Short Variable Declaration
package main
import (
"fmt"
)
func main() {
// Map
fmt.Println(" Map")
m := make(map[string]int)
m["one"] = 12
m["two"] = 05
fmt.Println("Map: ", m)
for key, value := range m {
fmt.Println("key:", key, " value:", value)
}
// Length Of Map
len := len(m)
fmt.Println("Length: ", len)
// Map Literal
fmt.Println("\n Map Literal")
ranks := map[string]int{"bronze": 3, "silver": 2, "gold": 1}
fmt.Println("Map Literal: ", ranks)
fmt.Println(ranks["gold"])
fmt.Println(ranks["silver"])
for key, value := range ranks {
fmt.Println("key:", key, " value:", value)
}
// Zero Values within maps
fmt.Println("\n Zero Values within maps: ", ranks["iron"])
value, ok := ranks["iron"]
//value, ok := ranks["gold"]
if !ok {
fmt.Println("\nThere is no such key as 'iron' in map")
} else {
fmt.Println("\nValue: ", value)
}
elements := map[string]string{
"H": "Hydrogen",
"Li": "Lithium",
}
fmt.Println("\nMap Literal: ", elements)
for key, value := range elements {
fmt.Println("key:", key, " value:", value)
}
fmt.Println("\n Map - Delete: Li")
delete(elements, "Li")
for key, value := range elements {
fmt.Println("key:", key, " value:", value)
}
}