Home >Backend Development >Golang >golang map to slice

golang map to slice

PHPz
PHPzOriginal
2023-05-10 10:13:361187browse

The map in Go language is an unordered key-value pair type, which can access the corresponding value according to the key. In some cases, we need to convert the data in the map to slice type to facilitate other operations. This article introduces several methods of converting golang map to slice.

Method 1: Use a for loop to traverse the map

By traversing the map through a for loop, you can add the key-value pairs in the map to the slice one by one. The specific code is as follows:

package main

import "fmt"

func main() {
    var m map[string]int
    m = make(map[string]int)
    m["one"] = 1
    m["two"] = 2
    m["three"] = 3

    keys := make([]string, 0, len(m))
    values := make([]int, 0, len(m))

    for key, value := range m {
        keys = append(keys, key)
        values = append(values, value)
    }

    fmt.Println(keys)   // [one two three]
    fmt.Println(values) // [1 2 3]
}

Method 2: Use the reflect library

reflect is a reflection library of the Go language, which can obtain type information, call methods, access structure fields, etc. The map can be converted to the reflect.Value type through the reflect.ValueOf() function, and then the key-value pairs can be obtained through the reflect.Value method. The specific code is as follows:

package main

import (
    "fmt"
    "reflect"
)

func main() {
    var m map[string]int
    m = make(map[string]int)
    m["one"] = 1
    m["two"] = 2
    m["three"] = 3

    keys := reflect.ValueOf(m).MapKeys()
    values := make([]int, 0, len(keys))
    for _, key := range keys {
        values = append(values, m[key.String()])
    }

    fmt.Println(keys)   // [one two three]
    fmt.Println(values) // [1 2 3]
}

Method 3: Use github.com/mitchellh/mapstructure library

mapstructure is a library for parsing structures, which can parse the key-value pairs in the map into structures. corresponding fields in . This library provides the Decode() function, which can parse map into any type of go value, including slice type. The specific code is as follows:

package main

import (
    "fmt"

    "github.com/mitchellh/mapstructure"
)

func main() {
    var m map[string]int
    m = make(map[string]int)
    m["one"] = 1
    m["two"] = 2
    m["three"] = 3

    values := make([]int, 0, len(m))
    err := mapstructure.Decode(m, &values)

    if err != nil {
        fmt.Println(err)
    } else {
        fmt.Println(values) // [1 2 3]
    }
}

Summary:

The above three methods can convert map to slice type. Among them, method one has relatively low performance and is suitable for small-scale map conversion. Although method two uses the reflection library, its efficiency is still higher than method one. Method three is to use a third-party library, which is convenient and fast, and is suitable for situations where the map data is relatively complex. We can choose different conversion methods according to specific scenarios.

The above is the detailed content of golang map to slice. 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