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

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

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

你可以这样做:

  1. var val map[string]int
  2. val, exists := m[key]
  3. if !exists {
  4. val = make(map[string]int)
  5. m[key] = val
  6. }

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

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

You can do:

  1. var val map[string]int
  2. val, exists := m[key]
  3. if !exists {
  4. val = make(map[string]int)
  5. m[key] = val
  6. }

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

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

答案2

得分: 2

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

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

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

  1. type Person map[string]int
  2. type City map[string]Person
  3. m := make(City)
  4. if _, ok := m["Dhaka"]; !ok {
  5. m["Dhaka"] = make(Person)
  6. }
英文:

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

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

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:

  1. type Person map[string]int
  2. type City map[string]Person
  3. m := make(City)
  4. if _, ok := m[&quot;Dhaka&quot;]; !ok {
  5. m[&quot;Dhaka&quot;] = make(Person)
  6. }

答案3

得分: 0

  1. func main() {
  2. var testMap map[int]interface{}
  3. testMap = make(map[int]interface{})
  4. var addMap map[int]string
  5. addMap = make(map[int]string)
  6. addMap[1] = "999"
  7. addMap[2] = "888"
  8. Add(testMap, 111, addMap)
  9. for key, val := range testMap {
  10. fmt.Println(key)
  11. for key2, val2 := range val.(map[int]string) {
  12. fmt.Println(key2, val2)
  13. }
  14. }
  15. }
  16. func Add(_testMap map[int]interface{}, _key int, _val map[int]string) {
  17. _, exist := _testMap[_key]
  18. if exist == false {
  19. _testMap[_key] = _val
  20. } else {
  21. // do whatever you want
  22. }
  23. }
  1. func main() {
  2. var testMap map[int]interface{}
  3. testMap = make(map[int]interface{})
  4. var addMap map[int]string
  5. addMap = make(map[int]string)
  6. addMap[1] = "999"
  7. addMap[2] = "888"
  8. Add(testMap, 111, addMap)
  9. for key, val := range testMap {
  10. fmt.Println(key)
  11. for key2, val2 := range val.(map[int]string) {
  12. fmt.Println(key2, val2)
  13. }
  14. }
  15. }
  16. func Add(_testMap map[int]interface{}, _key int, _val map[int]string) {
  17. _, exist := _testMap[_key]
  18. if exist == false {
  19. _testMap[_key] = _val
  20. } else {
  21. // do whatever you want
  22. }
  23. }
英文:
  1. func main() {
  2. var testMap map[int]interface{}
  3. testMap = make(map[int]interface{})
  4. var addMap map[int]string
  5. addMap = make(map[int]string)
  6. addMap[1] = &quot;999&quot;
  7. addMap[2] = &quot;888&quot;
  8. Add(testMap, 111, addMap)
  9. for key, val := range testMap {
  10. fmt.Println(key)
  11. for key2, val2 := range val.(map[int]string) {
  12. fmt.Println(key2, val2)
  13. }
  14. }
  15. }
  1. func Add(_testMap map[int]interface{}, _key int, _val map[int]string) {
  2. _, exist := _testMap[_key] // _ -&gt; value
  3. if exist == false {
  4. //addmap
  5. _testMap[_key] = _val
  6. } else {
  7. //whatever wanna to do
  8. }
  9. }

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:

确定