David's Blog

History of golang.

By David Li on Fri, 21 October 2024

In Go, arrays, slices, and maps are powerful data structures that are widely used in building applications. In this post, we will discuss these data structures in detail, including their differences and how to use them effectively.

Arrays

Arrays are a collection of elements of the same type, and their size is fixed at the time of declaration. You can declare an array in Go using the following syntax:

var arr [size]datatype

Here, size is the number of elements in the array, and datatype is the data type of each element. For example, to declare an array of integers with five elements, you can use the following code:

java`var arr [5]int

To access the elements of an array, you can use the following syntax:

arr[index]

Here, index is the index of the element you want to access, and it starts from 0. For example, to access the first element of the above array, you can use the following code:

arr[0]

Example: Iterating Over an Array

package main

import "fmt"

func main() {
 var arr [5]int = [5]int{1, 2, 3, 4, 5}

 for i := 0; i < len(arr); i++ {
 fmt.Println(arr[i])
 }
}

In this example, we declare an array of integers with five elements and assign some values to it. Then, we use a for loop to iterate over the array and print each element.

Slices

Slices are another type of array, but their size is not fixed, and they are more flexible than arrays. Slices are created from an existing array or a literal value, and they are a reference to the underlying array. You can declare a slice in Go using the following syntax:

var slice []datatype

Here, datatype is the data type of the elements in the slice. For example, to declare a slice of integers, you can use the following code:

var slice []int

To create a slice from an existing array, you can use the following syntax:

sql`slice := arr[start:end]

Here, start is the index of the first element in the slice, and end is the index of the last element plus one. For example, to create a slice from the first three elements of an array, you can use the following code:

slice := arr[0:3]

Example: Iterating Over a Slice

package main

import "fmt"

func main() {
 var arr [5]int = [5]int{1, 2, 3, 4, 5}
 var slice []int = arr[0:3]

 for i := 0; i < len(slice); i++ {
 fmt.Println(slice[i])
 }
}

In this example, we declare an array of integers with five elements and assign some values to it. Then, we create a slice from the first three elements of the array. Finally, we use a for loop to iterate over the slice and print each element.

Maps

Maps are a collection of key-value pairs, and they are similar to dictionaries in other programming languages. Maps are created using the following syntax:

var m map[keytype]valuetype

Here, keytype is the data type of the keys, and valuetype is the data type of the values. For example, to declare a map with string keys and integer values, you can use the following code:

var

An array is a fixed-size collection of elements of the same type. In Go, arrays are declared using the following syntax:

var arr [size]datatype

Here, size is the number of elements in the array, and datatype is the data type of each element. For example, to declare an array of integers with five elements, you can use the following code:

java`var arr [5]int

To access the elements of an array, you can use the following syntax:

arr[index]

Here, index is the index of the element you want to access, and it starts from 0. For example, to access the first element of the above array, you can use the following code:

arr[0]

To assign values to an array, you can use the following syntax:

arr[index] = value

For example, to assign the value 10 to the first element of the above array, you can use the following code:

arr[0] = 10

Example: Iterating Over an Array

package main

import "fmt"

func main() {
 var arr [5]int = [5]int{1, 2, 3, 4, 5}

 for i := 0; i < len(arr); i++ {
 fmt.Println(arr[i])
 }
}

In this example, we declare an array of integers with five elements and assign some values to it. Then, we use a for loop to iterate over the array and print each element.

Slices are a flexible and dynamic data structure in Go that can be used to store and manipulate a sequence of elements. In contrast to arrays, slices can grow and shrink in size during runtime.

A slice is a reference to an underlying array, and it consists of three parts: a pointer to the underlying array, a length, and a capacity. The length represents the number of elements in the slice, while the capacity is the maximum number of elements that the slice can hold.

You can create a slice in Go using the following syntax:

var slice []datatype

Here, datatype is the data type of the elements in the slice. For example, to declare a slice of integers, you can use the following code:

var slice []int

To create a slice from an existing array, you can use the following syntax:

sql`slice := arr[start:end]

Here, start is the index of the first element in the slice, and end is the index of the last element plus one. For example, to create a slice from the first three elements of an array, you can use the following code:

slice := arr[0:3]

To append an element to a slice, you can use the built-in append function. The append function takes a slice and one or more elements to append to the slice, and it returns a new slice with the appended elements.

slice = append(slice, element)

Here, element is the element you want to append to the slice.

Example: Iterating Over a Slice

package main

import "fmt"

func main() {
 var arr [5]int = [5]int{1, 2, 3, 4, 5}
 var slice []int = arr[0:3]

 for i := 0; i < len(slice); i++ {
 fmt.Println(slice[i])
 }
}

In this example, we declare an array of integers with five elements and assign some values to it. Then, we create a slice from the first three elements of the array. Finally, we use a for loop to iterate over the slice and print each element.

Maps are another fundamental data structure in Go that allow you to store and retrieve values using keys. In other programming languages, maps are sometimes called dictionaries, associative arrays, or hash tables.

A map is an unordered collection of key-value pairs, where each key is unique. The keys and values can be of any type, as long as the keys are comparable for equality using the == operator.

You can create a map in Go using the following syntax:

var m map[keytype]valuetype

Here, keytype is the data type of the keys, and valuetype is the data type of the values. For example, to create a map of strings to integers, you can use the following code:

var m map[string]int

To add a key-value pair to a map, you can use the following syntax:

m[key] = value

Here, key is the key you want to add, and value is the value associated with the key. For example, to add a key-value pair to the above map, you can use the following code:

m["one"] = 1

To retrieve a value from a map, you can use the following syntax:

value := m[key]

Here, key is the key you want to retrieve the value for, and value is the value associated with the key. For example, to retrieve the value associated with the key “one” from the above map, you can use the following code:

value := m["one"]

To delete a key-value pair from a map, you can use the built-in delete function. The delete function takes a map and a key to delete, and it removes the key-value pair from the map.

sdelete(m, key)

Here, key is the key you want to delete from the map.

Example: Creating and Using a Map

package main

import "fmt"

func main() {
 var m map[string]int = make(map[string]int)

 m["one"] = 1
 m["two"] = 2
 m["three"] = 3

 fmt.Println(m)

 delete(m, "two")

 fmt.Println(m)

 value, ok := m["one"]
 if ok {
 fmt.Println(value)
 }
}

In this example, we create an empty map of strings to integers using the make function. Then, we add some key-value pairs to the map, print the map, and delete a key-value pair from the map. Finally, we retrieve the value associated with the key “one” from the map and print it.

© Copyright 2024 by FriendlyUsers Tech Blog. Built with ♥ by FriendlyUser. Last updated on 2024-04-18.