The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
praynise

请教:使用 map[string]interface{}之后,虽然实现了够用来存储所有类型的 map,但是取出的值却没法直接用怎么破…

  •  
  •   praynise · Jan 24, 2018 · 2008 views
    This topic created in 3029 days ago, the information mentioned may be changed or developed.
    用 map[string]interface{}实现的 map,虽然可以放进去所有类型的数据,但是取出来的东西的类型都会变成 interface{},无法正常实现原有的使用,请问如何才能正常使用存入的数据呢?谢谢大家
    7 replies    2018-02-21 13:53:27 +08:00
    orvice
        1
    orvice  
       Jan 24, 2018
    类型推断
    sirgod
        2
    sirgod  
       Jan 24, 2018 via iPhone
    类型断言
    mason961125
        3
    mason961125  
       Jan 24, 2018
    其实没必要自己写解析器阿,有很多库可以用。传送门 https://godoc.org/github.com/buger/jsonparser
    hxtheone
        4
    hxtheone  
       Jan 25, 2018 via iPhone
    类型断言+1
    xvsfezz
        5
    xvsfezz  
       Jan 25, 2018
    类型断言,就是强制转换
    my3157
        6
    my3157  
       Jan 25, 2018
    go 语言吧, 说一下我的处理办法,
    声明一个 struct, struct 有私有字段 map[string]interface{}, 添加 Add Del Update 等 method , 对于常见类型, 添加 MustString, MustInt 等 method, 对于复杂结构体, 采用和 json 序列化类似的接口, 传入指针, 通过类型推断, 给指针赋值
    codingcrush
        7
    codingcrush  
       Feb 21, 2018
    我用的 simplejson
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1167 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 51ms · UTC 18:09 · PVG 02:09 · LAX 11:09 · JFK 14:09
    ♥ Do have faith in what you're doing.