dsewbh5588 2015-02-28 03:29
浏览 32
已采纳

可以包含任何类型的数据结构是什么?

Reference Go equivalent of a void pointer in C

this is a demo.

package main

import (
    "fmt"
)

func map_exist(map_val map[string]interface{}, key string) bool {
    _, ok := map_val[key]
    return ok
}

func main() {
    var one map[string][]string
    one["A"] = []string{"a", "b", "c"}
    fmt.Println(map_exist(one, "A"))

    var two map[string]string
    two["B"] = "a"
    fmt.Println(map_exist(two, "B"))
}

run result:

./main.go:15: cannot use one (type map[string][]string) as type map[string]interface {} in argument to map_exist

./main.go:19: cannot use two (type map[string]string) as type map[string]interface {} in argument to map_exist

I can not find a data structure to be able to contain any type .

  • 写回答

2条回答 默认 最新

  • dream518518518 2015-02-28 05:07
    关注

    It's true that interface{} is kind of like C's void *, but you can't implicitly convert a map[string]string into a map[string]interface{}. The key is to define your map objects at their declaration as map[string]interface{} objects. Unfortunately, you cannot assign new values to such a map, since Go can't know how much memory to allocate for each instance.

    func main() {
        one := map[string]interface{} {
            "A": []string{"a", "b", "c"},
        }
        fmt.Println(map_exist(one, "A"))
    
        two := map[string]interface{} {
            "B": "a",
        }
        fmt.Println(map_exist(two, "B"))
    }
    

    This works, but it probably isn't useful enough for your intended use case.

    Another approach is to define a generic map interface and make sure that the functions you intend to use are defined for the types you want to support. Basically, your goal is to build a library that's useful enough that you can reasonably offload the work of implementing your interface to the library's users. It would be nice if you could declare a default function or maybe just a macro to prevent the copy-paste, but I'm not aware of such a mechanism.

    Here's an example using an interface:

    type GenericMap interface{
        ValueForKey(string) (interface{}, bool)
    }
    
    type SliceMap map[string][]string
    type StringMap map[string]string
    type VoidMap map[string]interface{}
    
    func map_exist(map_val GenericMap, key string) bool {
        _, ok := map_val.ValueForKey(key)
        return ok
    }
    
    func (map_val SliceMap) ValueForKey(key string) (interface{}, bool) {
        val, ok := map_val[key]
        return val, ok
    }
    
    func (map_val StringMap) ValueForKey(key string) (interface{}, bool) {
        val, ok := map_val[key]
        return val, ok
    }
    
    func (map_val VoidMap) ValueForKey(key string) (interface{}, bool) {
        val, ok := map_val[key]
        return val, ok
    }
    
    func main() {
        one := SliceMap {
            "A": []string{"a", "b", "c"},
        }
        fmt.Println(map_exist(GenericMap(one), "A"))
    
        two := StringMap {
            "B": "a",
        }
        fmt.Println(map_exist(GenericMap(two), "B"))
    }
    

    In the end, Go programmers tend to prefer a simple, straight-forward solution over all this misdirection, so your best bet is to just use the construct proposed by @Logiraptor:

    if _, ok := m[key]; ok {
        // You're ok!
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗