Home Backend Development Golang Go language map using custom types as keys

Go language map using custom types as keys

Mar 24, 2024 pm 05:12 PM
go language map Custom type key key value pair

Go language map using custom types as keys

Title: Go language map example using custom type as key

In Go language, you can use custom type as key of map, which provides us A more flexible data storage method. By defining custom types, more complex key-value relationships can be implemented to meet specific needs. In this article, we will introduce how to use custom types as map keys in Go language and provide specific code examples.

First, we need to define a custom type as the key of the map. Here we take a structure type as an example:

package main

import "fmt"

type Coordinate struct {
    X int
    Y int
}

func main() {
    // 创建一个以Coordinate为键,字符串为值的map
    coordinateMap := make(map[Coordinate]string)

    // 初始化Coordinate作为键的值
    coord1 := Coordinate{X: 1, Y: 2}
    coord2 := Coordinate{X: 3, Y: 4}

    // 将键值对添加到map中
    coordinateMap[coord1] = "A"
    coordinateMap[coord2] = "B"

    // 获取特定键对应的值
    fmt.Println("coord1对应的值为:", coordinateMap[coord1])
    fmt.Println("coord2对应的值为:", coordinateMap[coord2])

    // 循环遍历map
    for key, value := range coordinateMap {
        fmt.Printf("坐标(%d,%d)对应的值为:%s
", key.X, key.Y, value)
    }
}
Copy after login

In the above code, we define a structure type Coordinate, containing two integer fields X and Y. Then create a mapcoordinateMap with Coordinate as the key and string as the value, and add two sets of key-value pairs to it. Finally, for range loops through the map and outputs the value corresponding to each key value.

Using custom types as map keys allows us to process complex data structures more conveniently and improves the readability and ease of use of the code. Through the above examples, we can see how to use custom types as map keys in the Go language. I hope it will be helpful to you.

The above is the detailed content of Go language map using custom types as keys. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The difference between performance testing and unit testing in Go language The difference between performance testing and unit testing in Go language May 08, 2024 pm 03:09 PM

The difference between performance testing and unit testing in Go language

What pitfalls should we pay attention to when designing distributed systems with Golang technology? What pitfalls should we pay attention to when designing distributed systems with Golang technology? May 07, 2024 pm 12:39 PM

What pitfalls should we pay attention to when designing distributed systems with Golang technology?

How to define and use custom types using Go language? How to define and use custom types using Go language? Jun 05, 2024 pm 12:41 PM

How to define and use custom types using Go language?

Golang technology libraries and tools used in machine learning Golang technology libraries and tools used in machine learning May 08, 2024 pm 09:42 PM

Golang technology libraries and tools used in machine learning

Java data structures and algorithms: in-depth explanation Java data structures and algorithms: in-depth explanation May 08, 2024 pm 10:12 PM

Java data structures and algorithms: in-depth explanation

Things to note when Golang functions receive map parameters Things to note when Golang functions receive map parameters Jun 04, 2024 am 10:31 AM

Things to note when Golang functions receive map parameters

The role of Golang technology in mobile IoT development The role of Golang technology in mobile IoT development May 09, 2024 pm 03:51 PM

The role of Golang technology in mobile IoT development

How to create immutable custom types in Golang? How to create immutable custom types in Golang? Jun 02, 2024 am 09:41 AM

How to create immutable custom types in Golang?

See all articles