duanquan1207 2013-08-19 03:52
浏览 15
已采纳

在Go中,哪种值是体现。接口?

j:=1

Kind of j is reflect.Int, as expected.

var j interface{} = 1

Kind of j is also reflect.Int.

Which value’s kind is reflect.Interface?

  • 写回答

7条回答 默认 最新

  • douzhang3822 2013-08-19 14:25
    关注

    If you're looking for a practical solution, the answer is simple and annoying. reflect.TypeOf takes an empty interface type into which you put whatever data you want to pass. The problem with this is that an interface type can't hold another interface type, which means that you effectively can't pass an interface to reflect.TypeOf. There is a workaround, but it's a bit painful. What you have to do is make a composite type (like a struct or slice or map) where one of the element types is an interface type, and extract it. For example:

    var sliceOfEmptyInterface []interface{}
    var emptyInterfaceType = reflect.TypeOf(sliceOfEmptyInterface).Elem()
    

    That first creates a reflect.Type representation of []interface{} (a slice of interface{} types) and then extracts the element type, which is interface{}.

    Courtesy of this post.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入