how to get values from a interface in golang

Viewed 4788

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 Answers
Related