This is the 11th day of my participation in Gwen Challenge

These reviews

Previous articles covered the basic concepts and use of arrays and slicing for Go containers. This article will cover the use of lists and dictionaries in the Go language.

Lists and dictionaries

Golang’s list is implemented as a two-way linked list, enabling efficient insertion and deletion of elements. The initialization style of the list looks like this:

var name list.List
// or
name := list.New()
Copy the code

We can either declare the initializer directly or use the New function in the container/list package, which returns the pointer to the list. Notice that the list has no restrictions on the types of members it can hold, meaning that members of any type can exist in the list at the same time.

Let’s use a simple example to demonstrate insert, delete, and iterate over a list, as follows:

package main

import (
	"container/list"
	"fmt"
)

func main(a)  {
	tmpList := list.New()

	for i:= 1 ; i <= 10 ; i++ {
		tmpList.PushBack(i)
	}

	first := tmpList.PushFront(0)
	tmpList.Remove(first)

	forl := tmpList.Front(); l ! =nil; l = l.Next(){
		fmt.Print(l.Value, "")}}Copy the code

Each insert to the list returns a *list.Element structure that points to the node where the value is currently inserted. To Remove, move, or insert a member of the list, you need to use the specified *list. The list is iterated slightly differently from other containers, fetching the first element of the list with Front and then using its Next function to iterate down, as shown above.

The mapping container provided in Golang is a map, which is internally implemented as a hash table. Define a map as follows:

name := make(map[keyType]valueType)
Copy the code

The map needs to be initialized using the make function, where keyType is the keyType and valueType is the valueType of the key. We’ll go through a simple example of how to use a style map, as follows:

package main

import "fmt"

func main(a) {

	classMates1 := make(map[int]string)

	// Add the mapping
	classMates1[0] = "Xiao Ming"
	classMates1[1] = "Little red"
	classMates1[2] = "Zhang"

	// Get value based on key
	fmt.Printf("id %v is %v\n".1, classMates1[1])

	// Initialize data at declaration time
	classMates2 := map[int]string{
		0 : "Xiao Ming".1 : "Little red".2 : "Zhang",
	}

	fmt.Printf("id %v is %v\n".3, classMates2[3])}Copy the code

As shown in the code above, we can use the make function to construct the map and then use key-value pairs to add members to the map, or we can directly define the contents in jSON-like format during declaration. The corresponding value can be queried directly by key, and the default value of the value type will be returned if no such key exists. You can check whether a key exists in the map in the following way:

mate,ok := classMate2[1]
Copy the code

Boolean OK will be true if the key exists in the map.

summary

This paper mainly introduces the basic use of lists and dictionaries. Go language provides list list and Map mapping containers, which are commonly used in our daily life. The implementation of list is based on bidirectional linked lists. A Map is an unordered collection of key-value pairs. One of the most important aspects of a Map is its ability to quickly retrieve data through keys, which are similar to indexes that point to the value of the data.

In the following articles, we will continue to introduce the knowledge of containers: traversal of containers.