Array, Slice and Map

Array

An array is a numbered sequence of elements of a specific length. Each element in array is the same data type. By default, when you declare a array with any data type, each element in array has value is default of data type

Example 1: this example declare an array with 5 elements with value [0, 0, 0, 0, 0]. After that, you can fmt.Println array to view result

var arrayInt [5]int
fmt.Println("declared array:", arrayInt)

declared array: [0 0 0 0 0]

Example 2: other example with array 3 elements of string

var arrayStri [3]string
fmt.Println("declared new array:", arrayStri)
declared new array: ["" "" ""]

We can set a value at an index using the array[index] = value syntax, and get a value with array[index]. Note: index start with 0(1st element has index = 0)

var arrayInt [5]int
arrayInt[2] = 19
fmt.Println("declared array:", arrayInt)
fmt.Println("get value at 2:", arrayInt[2])

declared array: [0 0 19 0 0]
get value at 2: 19

Golang has a syntax to get length of array by function “len”

lengthOfArray := len(arrayInt)
fmt.Println("Length of array:", lengthOfArray)
Length of array: 5

If you use array, you will feel unhappy with length of array. What happen if you want array with flexible length. You’ll see slices much more often than arrays in typical Go. We’ll look at slices next.

Slice

Slices are a key data type in Go, giving a more powerful interface to sequences than arrays. Unlike arrays, slices are typed only by the elements they contain (not the number of elements). To create an empty slice with non-zero length, use the builtin make. Here we make a slice of strings of length 3 (initially zero-valued).

This is an example using “make” to create slice with init length = 3

sliceString := make([]string, 3)
fmt.Println("init slice:", sliceString)
init slice: ["" "" ""]

Look like array, you can use index and len in slice

sliceString[0] = "hello"
sliceString[1] = "world"
sliceString[2] = "how2code"
fmt.Println("new data:", sliceString)
fmt.Println("get:", sliceString[2])
new data: ["hello" "world" "how2code"]
get: ["how2code"]

In addition to these basic operations, slices support several more that make them richer than arrays. One is the builtin append, which returns a slice containing one or more new values. Note that we need to accept a return value from append as we may get a new slice value.

sliceString = append(sliceString, "thaibao56@gmail.com")
sliceString = append(sliceString, "1", "2")
fmt.Println("after append:", sliceString)
after append: ["hello" "world" "how2code" "thaibao56@gmail.com" "1" "2"]

Slices can also be copy. You can copy elements from this slice to that slice. Here, I create an empty slice copySlice with the same length of sliceString, and copy data from sliceString into copySlice, you try to fmt.Println(copySlice) and view result

copySlice := make([]string, len(sliceString))
copy(copySlice, sliceString)

Slices support a “slice” operator with the syntaxslice[low:high].  Try it.

sub := sliceString[1:2]
fmt.Println(sub)

sub = sliceString[:3]
fmt.Println(sub)

sub = sliceString[1:]
fmt.Println(sub)

More advanced, slice of slice

advance := make([][]int, 10)
advance[2][1] = 12

fmt.Println(advance)

Map

Maps are Go’s built-in associative data type (sometimes called hashes or dicts in other languages). Simple, I often call this data-type is key-value data-type. Why? Please view syntax

mapVariableName := make(map[key-type]val-type)

To create an empty map, use the builtin make:make(map[key-type]val-type). Here, I created a map1 with key is string and value is integer

To set key/value pairs using typical name[key] = val syntax.

And with key, you can get value at key of map

Because you set 3 keys for map1, so lenght of map1 is 3 and you can get by len build-in function of golang look like array or slice

How about “delete”? Nice, use delete build-in of golang with specific key you want to delete from map1

map1 := make(map[string]int)





map1["key1"] = 21
map1["key2"] = 6
map1["key3"] = 1987

// print all map1
fmt.Println("map:", map1)


// get value of key1
valueOfKey1 := map1["key1"]
fmt.Println(valueOfKey1)


// print length of map
lengthOfMap := len(map1)
fmt.Println(lengthOfMap)


// after delete key2, we only have key1 and key3
delete(map1, "key2")
fmt.Println("map:", map1)

Advance declaration with map. You can create map with initialization key-value pairs of map

map2 := map[string]int{"how2code": 1, "thaibao.how2codegmail.com": 2}
fmt.Println("map:", map2)

View on github

PreNext