Unmarshalling JSON into Go interface{} -


I have an area of ​​this type like interface {} . In the process of caching it using memcached (), the straat is inserted in JSON, which opens back into the structure when it is received from the cache. As a result, the interface {} field essentially indicates for a type of map [string] interface {} (as interface {} field specified as map only The [string] interface {}), the type of information is not kept for the marshaling and the unsampling process. Is there any way to save this information in the martial process, in such a way that it can be unhappy? Or to use any other codec or something else? Type A struct {name string id string} func main () {a: = A {value: b} {name: "hi", id: "12345"}} Cache. Set ("A", A) // Marshall 'A' in cache and cache results in cache = cache. ("A") / / Cache and unmodular FMT print from "A" ("% s", Result.V. (B) .name) // Generates error saying that // map [ String] interface {} A 'B' struct fmt.printf ("% s", result.value. (Map [string] interface {}) ["name"]. (String)) // correctly "12345" Printed

short version, no you can not do it, though you have some options Are there. Change avw to use B instead of

    interface {} .
  1. Add function to the code> A which changes A.Value from one map to B , if it is first Not just B . Use
  2. encoding / gob and store b in the memcache then it will be newA (b [] byte * A . To use gob like

    , you must register each straight before encoding / decoding:

      func init () {// where you should register in your own way, just once gob.Register (a {}) gob .Register (B {}}} func main () {var (buf bytes.buffernink = Gob NewInkord (& Amp; buff) DC = Gob. Nudicoder (& amp; amp; buf) val = A {b} {name}, "id"}} ra fmt.println (enc.Encode (& amp;)) fmt.println Dec.Decode (& amp; r)) fmt.printf ("% # v", r)}    

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -