I found what I want
https://github.com/lunixbochs/struc
https://github.com/go-restruct/restruct

2017-11-07 18:33 GMT+08:00 Konstantin Khomoutov <kos...@bswap.ru>:

> On Tue, Oct 31, 2017 at 04:36:20PM +0800, hui zhang wrote:
>
> [...]
> > > > Is there a way or lib to read a binary into a this structure in one
> line
> > > > code?
> > > >
> > > > how about more complicate struct ?
> > > > type complicatestrt struct {
> > > >       len int32
> > > >       strData []variastrt
> > > > }
> [...]
> > > - Write some complicated code which uses reflection -- just like
> > >   the stock encoding/binary does -- to understand how to interpret the
> > >   fields of any custom struct type based on that type's tags (or just
> > >   "shape" -- that is, the types and relative order of its fields).
> > >
> > > For the latter, take into account that while there's no mention of the
> > > format of the field tag in the language spec, the convention for their
> > > format is documented at [4]:
> > >
> > > | By convention, tag strings are a concatenation of optionally
> > > | space-separated key:"value" pairs. Each key is a non-empty string
> > > | consisting of non-control characters other than space (U+0020 ' '),
> > > | quote (U+0022 '"'), and colon (U+003A ':'). Each value is quoted
> using
> > > | U+0022 '"' characters and Go string literal syntax.
> > >
> > > Hence you could come up with something like:
> > >
> > >   type variastrt struct {
> > >       len  int32   `var:"value:data endian:big"`
> > >           data []int32 `var:"endian:little"`
> > >   }
> > >
> > > And then you'd reflect over the type the user supplied to your
> > > unmarshaling code, and in that reflection code you'd parse the field's
> > > tag, get the value associated with the "var" key and parse that, in
> > > turn, to know the name of the field to unmarshal the value into, and
> the
> > > endianness of the elements of that value (if applicable).
> [...]
> > > 4. https://golang.org/pkg/reflect/#StructTag
>
> > Thank you very much
> > I know something like below could do.  The problem has some one did it ?
> I
> > don't know how to use the tag string.
> >  type variastrt struct {
> >       len  int32   `var:"value:data endian:big"`
> >           data []int32 `var:"endian:little"`
> >   }
>
> I'm not sure what you're asking about.
> As I have said, if you would like to implement the indicated approach,
> you would need to write code which obtains tags defined on the fields of
> a user-programmed type, parses them and acts based on the results.
>
> I have shown you what is the codified policy on the format of the struct
> field tags (they are not defined by the Go language specification but
> everyone agrees upon that policy which I cited in my first reply to this
> thread).  To inspect at runtime the type of a user-supplied variable,
> and then further inspect its properties — such as fields of a struct
> type, you need to use the standard package "reflect".
>
> If you want to learn how to reflect over any custom user-defined type
> using the standard package "reflect" and deal with the tags defined on
> the fields of such a type, I'd suggest looking at the standard package
> encoding/json and encoding/xml which make heavy use of these tags.
>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "golang-nuts" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/
> topic/golang-nuts/Nn5ZuGIXDUM/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> golang-nuts+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to