将指针或布尔值的映射转换为接口的映射。

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

go: map of pointers or bools as map of interface

问题

假设我有不同类型的地图,其中包括布尔类型和指针类型,并且想要将它们以一种统一的方式传递给某个函数:

  1. type Blah struct {
  2. name string
  3. }
  4. var mapBlah map[string]*Blah = make(map[string]*Blah)
  5. var mapBool map[string]bool = make(map[string]bool)
  6. func joinKeys(m map[string]interface{}) string {
  7. // 返回字符串.Join(map的键)
  8. }

我似乎无法将这些地图传递给这个函数(示例)。如果我漏掉了什么,应该如何正确地完成这个操作,或者为什么不能完成呢?

英文:

Suppose I have maps of different types, among them of bools and of pointers and want to pass them to some function in single way:

  1. type Blah struct {
  2. name string
  3. }
  4. var mapBlah map[string]*Blah = make(map[string]*Blah)
  5. var mapBool map[string]bool = make(map[string]bool)
  6. func joinKeys(m map[string]interface{}) string {
  7. // returns strings.Join(keys of map)
  8. }

I seemingly can't pass these maps into this function (example). How can this be done properly, or
why it can't be done if I'm missing something, please?

答案1

得分: 1

当你已经知道类型时,我认为像下面这样切换类型会很有益处。

  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. type Blah struct {
  7. name string
  8. }
  9. var mapBlah map[string]*Blah = make(map[string]*Blah)
  10. var mapBool map[string]bool = make(map[string]bool)
  11. func joinKeys(m interface{}) string {
  12. var a []string
  13. switch v := m.(type) {
  14. case map[string]*Blah:
  15. for k, _ := range v {
  16. a = append(a, k)
  17. }
  18. case map[string]bool:
  19. for k, _ := range v {
  20. a = append(a, k)
  21. }
  22. default:
  23. }
  24. return strings.Join(a, ",")
  25. }
  26. func main() {
  27. mapBlah["1B"] = &Blah{name: "first"}
  28. mapBlah["2B"] = &Blah{name: "second"}
  29. fmt.Println(joinKeys(mapBlah))
  30. mapBool["1Bool"] = true
  31. fmt.Println(joinKeys(mapBool))
  32. }

对于更具动态性但性能有所牺牲的方式,可以使用reflection

英文:

When you already know the types, I think switching over types will be beneficial like follows.

  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. type Blah struct {
  7. name string
  8. }
  9. var mapBlah map[string]*Blah = make(map[string]*Blah)
  10. var mapBool map[string]bool = make(map[string]bool)
  11. func joinKeys(m interface{}) string {
  12. var a []string
  13. switch v := m.(type) {
  14. case map[string]*Blah:
  15. for k, _ := range v {
  16. a = append(a, k)
  17. }
  18. case map[string]bool:
  19. for k, _ := range v {
  20. a = append(a, k)
  21. }
  22. default:
  23. }
  24. return strings.Join(a, ",")
  25. }
  26. func main() {
  27. mapBlah["1B"] = &Blah{name: "first"}
  28. mapBlah["2B"] = &Blah{name: "second"}
  29. fmt.Println(joinKeys(mapBlah))
  30. mapBool["1Bool"] = true
  31. fmt.Println(joinKeys(mapBool))
  32. }

For more dynamic way with some performance tradeoffs reflection is way to go.

huangapple
  • 本文由 发表于 2021年8月7日 15:27:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/68690253.html
匿名

发表评论

匿名网友

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

确定