Home  >  Article  >  Backend Development  >  golang array to map

golang array to map

WBOY
WBOYOriginal
2023-05-15 09:32:061041browse

In the Go language, it is sometimes necessary to convert an array into a map type. This article will introduce how to implement this function.

An array is a set of data types arranged in a certain order, while map is a data type stored in the form of key-value pairs. Arrays can access elements using subscripts, while maps require keys to access values. In some scenarios, we need to convert the array into a map type to more conveniently access the elements.

For example, we have an array with a length of 5, which stores the names and corresponding grades of 5 students. We hope to convert it into a map type so that we can quickly find the corresponding grades based on the student's name.

First, we need to define a type to store the names and grades of students:

type Student struct {
    Name  string
    Score int
}

Then, we create an array of length 5 to store the names and grades of 5 students:

students := [5]Student{
    {Name: "张三", Score: 90},
    {Name: "李四", Score: 85},
    {Name: "王五", Score: 78},
    {Name: "赵六", Score: 92},
    {Name: "钱七", Score: 88},
}

Then, we define a map type, using the student name as the key and the student grade as the value:

scores := make(map[string]int)

Next, we traverse each element in the array, using the student name as the key, and the student The grades are stored in scores as values:

for _, student := range students {
    scores[student.Name] = student.Score
}

Finally, we can find the corresponding grades in scores according to the student’s name:

score, ok := scores["张三"]
if ok {
    fmt.Printf("张三的成绩为:%d
", score)
} else {
    fmt.Println("未找到张三的成绩")
}

The complete code is as follows:

package main

import "fmt"

type Student struct {
    Name  string
    Score int
}

func main() {
    students := [5]Student{
        {Name: "张三", Score: 90},
        {Name: "李四", Score: 85},
        {Name: "王五", Score: 78},
        {Name: "赵六", Score: 92},
        {Name: "钱七", Score: 88},
    }

    scores := make(map[string]int)
    for _, student := range students {
        scores[student.Name] = student.Score
    }

    score, ok := scores["张三"]
    if ok {
        fmt.Printf("张三的成绩为:%d
", score)
    } else {
        fmt.Println("未找到张三的成绩")
    }
}

Output The result is:

张三的成绩为:90

Through the above code example, we can see that converting an array to a map is very simple. You only need to define the key-value pair type, create the map object, traverse the array, and fill the elements into the map.

Summary:

  • In Go language, you can convert an array into a map type by traversing it.
  • During the conversion process, the key-value pair type and map object need to be defined.
  • After conversion, you can quickly find the corresponding value based on the key.

The above is the detailed content of golang array to map. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn