Golang中的map如何实现”put if absent”的功能?

huangapple go评论103阅读模式
英文:

Golang map put if absent?

问题

我有一个格式为 map[string]map[string]int 的地图。

在这个主地图中,我想要做类似于 Java 中的 putIfAbsent("key", new HashMap<>() 的操作。在 Go 语言中,有没有一种简洁的方式来实现这个功能呢?

英文:

I have a map of the format:

map[string]map[string]int

In this main map, I want to do something like putIfAbsent(&quot;key&quot;, new HashMap&lt;&gt;() as we have in Java. What is a clean and shorthand way to do it in Go?

答案1

得分: 7

你可以这样做:

var val map[string]int
val, exists := m[key]
if !exists {
	val = make(map[string]int)
	m[key] = val
}

如果你在下面的代码中不需要 val

if _, exists := m[key]; !exists {
    m[key] = make(map[string]int)
}
英文:

You can do:

var val map[string]int
val, exists := m[key]
if !exists {
	val = make(map[string]int)
	m[key] = val
}

If you don't need the val in the code coming below this:

if _,exists := m[key]; !exists {
    m[key]=make(map[string]int)
}

答案2

得分: 2

如果您不打算立即使用该值,这里是代码的翻译部分:

m := make(map[string]map[string]int)

if _, ok := m["unknown"]; !ok {
    m["unknown"] = make(map[string]int)
}

以下是改进建议:
为了保持代码整洁和易于理解,您可以定义自己的类型。例如,如果您的数据是一个映射,表示“城市到人到年龄”,我会这样做:

type Person map[string]int
type City map[string]Person

m := make(City)

if _, ok := m["Dhaka"]; !ok {
    m["Dhaka"] = make(Person)
}
英文:

If you don't intend to use the value right away, here you go...

m := make(map[string]map[string]int)

if _, ok := m[&quot;unknown&quot;]; !ok {
    m[&quot;unknown&quot;] = make(map[string]int)
}

Below is a suggestion for improvement:
To keep things clean and easy to understand, you can define your own types. For example, if your data is a mapping of "cities to persons to age", I would do it like this:

type Person map[string]int
type City map[string]Person

m := make(City)

if _, ok := m[&quot;Dhaka&quot;]; !ok {
    m[&quot;Dhaka&quot;] = make(Person)
}

答案3

得分: 0

func main() {
	var testMap map[int]interface{}
	testMap = make(map[int]interface{})

	var addMap map[int]string
	addMap = make(map[int]string)
	addMap[1] = "999"
	addMap[2] = "888"

	Add(testMap, 111, addMap)

	for key, val := range testMap {
		fmt.Println(key)
		for key2, val2 := range val.(map[int]string) {
			fmt.Println(key2, val2)
		}
	}
}

func Add(_testMap map[int]interface{}, _key int, _val map[int]string) {
	_, exist := _testMap[_key]
	if exist == false {
		_testMap[_key] = _val
	} else {
		// do whatever you want
	}
}
func main() {
	var testMap map[int]interface{}
	testMap = make(map[int]interface{})

	var addMap map[int]string
	addMap = make(map[int]string)
	addMap[1] = "999"
	addMap[2] = "888"

	Add(testMap, 111, addMap)

	for key, val := range testMap {
		fmt.Println(key)
		for key2, val2 := range val.(map[int]string) {
			fmt.Println(key2, val2)
		}
	}
}

func Add(_testMap map[int]interface{}, _key int, _val map[int]string) {
	_, exist := _testMap[_key]
	if exist == false {
		_testMap[_key] = _val
	} else {
		// do whatever you want
	}
}
英文:
func main() {
	var testMap map[int]interface{}
	testMap = make(map[int]interface{})

	var addMap map[int]string
	addMap = make(map[int]string)
	addMap[1] = &quot;999&quot;
	addMap[2] = &quot;888&quot;

	Add(testMap, 111, addMap)

	for key, val := range testMap {
		fmt.Println(key)
		for key2, val2 := range val.(map[int]string) {
			fmt.Println(key2, val2)
		}
	}
}
func Add(_testMap map[int]interface{}, _key int, _val map[int]string) {
	_, exist := _testMap[_key] // _ -&gt; value
	if exist == false {
		//addmap
		_testMap[_key] = _val
	} else {
		//whatever wanna to do
	}
}

huangapple
  • 本文由 发表于 2021年6月4日 08:23:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/67830064.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定