【问题标题】:How do you extract a value out of a golang map[string] string thats typed with interface{} [duplicate]如何从使用 interface{} 键入的 golang map[string] 字符串中提取值 [重复]
【发布时间】:2019-06-16 05:12:27
【问题描述】:

我有这样的事情:

x1 := someFunctionWithAnInterfaceReturnValue()

底层类型是这样的:

x2 := map[string] string{"hello": "world"}

如何访问 x1 中的值?

基本上我想要 x1 的等效项:

 var value string = x2["hello"]

【问题讨论】:

    标签: dictionary go types


    【解决方案1】:

    使用type assertion

    x1 := someFunctionWithAnInterfaceReturnValue()
    x2, ok := x1.(map[string]string)
    if !ok {
       // handle unexpected type
    }
    var value string = x2["hello"]
    

    【讨论】:

      猜你喜欢
      • 2016-06-09
      • 1970-01-01
      • 2018-12-20
      • 2021-01-02
      • 2018-08-28
      • 2019-01-18
      • 2021-11-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多