P
P
P7472021-09-28 13:13:36
go
P747, 2021-09-28 13:13:36

Accessing a specific value of a given library?

Good afternoon!
I'm just learning Go syntax. Can you please tell me how to get a certain value from the map of this library? That is, it displays map in the console, I tried some options from the Internet, it didn’t work :(
https://github.com/kovetskiy/go-php-serialize

https://play.golang.org/p/b4pPAaTwMAA

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Mamonov, 2021-09-28
@P747

This is how you get there

package main

import (
    "fmt"

    php "github.com/kovetskiy/go-php-serialize"
)

func main() {
    s := `a:6:{s:7:"__flash";a:1:{s:3:"mes";s:5:"Hello";}s:4:"__id";s:36:"3c5035d9-aea1-4f08-8325-9e598921e2a9";}`
    val, err := php.Decode(s)
    if err != nil {
        fmt.Println(err)
        return
    }

    session := val.(map[interface {}]interface {})
    flash := session["__flash"].(map[interface {}]interface{})

    fmt.Println("id", session["__id"])
    fmt.Println("mes", flash["mes"])
}

For good, you need to check whether the type conversion went well so that there is no panic
session, ok := val.(map[interface {}]interface {})
    if !ok {
        // с val явно что-то не так, оно не соответствует типу map[interface {}]interface {}
    }

    flash, ok := session["__flash"].(map[interface {}]interface{})
    if !ok {
        // ...
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question