duanlang1196 2017-08-16 05:49
浏览 366
已采纳

如何从golang中的接口获取值

I need to get values from a serialized string which generated from php code

So I use a package named:php_serialize to unserialize the string and then got a result of interface{} type .

But I have no idea how to get values inside the result.

This is code:

package main

import (
    "github.com/yvasiyarov/php_session_decoder/php_serialize"
    "fmt"
)

func main() {
    // this string is generated from php code
    str := `a:3:{s:4:"name";s:3:"tom";s:3:"age";s:2:"23";s:7:"friends";a:2:{i:0;a:1:{s:4:"name";s:5:"jerry";}i:1;a:1:{s:4:"name";s:4:"jack";}}}`

    decoder := php_serialize.NewUnSerializer(str)

    if result, err := decoder.Decode(); err != nil {
        panic(err)
    } else {
        fmt.Println(result)
    }
}

The print result is :

map[name:tom age:23 friends:map[0:map[name:jerry] 1:map[name:jack]]]

This result is a php_serialize.PhpValue type, which is interface{} type

The next step is how to get values inside the result?

such as get the age field and value

  • 写回答

2条回答 默认 最新

  • douye2572 2017-08-16 09:17
    关注

    Here some ways to access the data:

    package main
    
    import (
        "fmt"
    
        "github.com/yvasiyarov/php_session_decoder/php_serialize"
    )
    
    func main() {
        // this string is generated from php code
        str := `a:3:{s:4:"name";s:3:"tom";s:3:"age";s:2:"23";s:7:"friends";a:2:{i:0;a:1:{s:4:"name";s:5:"jerry";}i:1;a:1:{s:4:"name";s:4:"jack";}}}`
    
        decoder := php_serialize.NewUnSerializer(str)
    
        result, err := decoder.Decode()
        if err != nil {
            panic(err)
        }
        fmt.Println(result)
    
        // simple assert
        t := result.(php_serialize.PhpArray)
        // use php_seriale build in function to get string
        strVal := php_serialize.PhpValueString(t["name"])
        fmt.Println(strVal)
    
        // type switch in case of different valid types
        switch t := result.(type) {
        default:
            fmt.Printf("unexpected type %T
    ", t) // %T prints whatever type t has
        case php_serialize.PhpArray:
            fmt.Println(t)
            fmt.Println(t["name"])
            fmt.Println(t["age"])
    
            // should be done recursively...
            switch f := t["friends"].(type) {
            default:
                fmt.Printf("unexpected type %T
    ", f) // %T prints whatever type t has
            case php_serialize.PhpArray:
                fmt.Println(f)
                fmt.Println(f[0])
                fmt.Println(f[1])
            }
        }
    }
    

    I hope this gives you some ideas.

    Basic concept

    • php_serialize has built in functions to convert primitives.
    • Variable structures are represented with built in types which need to be used to access the structure.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!