ZBLOG

golang m.Range

m.Range is a method in the Go programming language that is used to iterate over all key-value pairs stored in a map. It takes as input a function that will be called for each key-value pair in the map.

The syntax for using m.Range is:

func (m mapType) Range(f func(key keyType, value valueType) bool)

Here, mapType represents the type of the map, keyType and valueType represent the types of the keys and values in the map, respectively. The function f takes two arguments: a key and a value, and returns a boolean value indicating whether the iteration should continue or not.

Example:

package main

import (
    "fmt"
)

func main() {
    m := make(map[string]int)
    m["foo"] = 1
    m["bar"] = 2

    // Iterate over the map
    m.Range(func(key string, value int) bool {
        fmt.Printf("Key: %s, Value: %d\n", key, value)
        return true
    })
}

Output:

Key: foo, Value: 1
Key: bar, Value: 2

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=650

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?