Translation language: how to serialize a complex interface with outstanding fields?

I need to serialize some complex interface (template.Template). It has many unexposed fields, and gob does not want to work with them. Any suggestions?

PS In fact, I'm trying to put the parsed template into memcache in App Engine.

+4
source share
2 answers

The short answer is that there is usually a reason for unexposed fields: template.Template , for example, contains information that changes during parsing - - I would advise you not to serialize them yourself using reflect . However, the GobEncoder and GobDecoder have recently been added to gob ; if you need to serialize a complex structure with non-extended fields, ask the package author to implement these interfaces. Better yet, implement them yourself (it shouldn't be difficult for template.Template ) and contribute your patch.

+5
source

If a type from another package (for example, a template) cannot be executed with any of the current serialization libraries for Go ( gob , json , bson, etc.). This also should not be done since the fields are not shown.

However, if you really need to, you can write your own serializer using the reflect package, in particular Value.Field() and friends, to get unallocated fields. Then you just need to save them so that you can decode later.

+1
source

All Articles