有没有类似于Python构造的golang库? [关闭]

I really love the Python construct module's declarative syntax for defining bi-directional (binary|text) parser / builders.

I've recently started focusing on golang and was wondering if anyone has seen (or might be the esteemed author of) a similar library for golang.

If you've never used the construct module, you basically build a declarative tree of Python objects that you can feed Python object trees and get binary blobs out, or parse binary blobs into Python object trees.

A simple example from the construct webpage:

>>> PascalString2 = ExprAdapter(PascalString,
...     encoder = lambda obj, ctx: Container(length = len(obj), data = obj),
...     decoder = lambda obj, ctx: obj.data
... )
>>> PascalString2.parse("\x05hello")
'hello'
>>> PascalString2.build("i'm a long string")
"\x11i'm a long string"

A slightly more complex example from the source that shows a hard drive MBR parser.

mbr = Struct("mbr",
    HexDumpAdapter(Bytes("bootloader_code", 446)),
    Array(4,
        Struct("partitions",
            Enum(Byte("state"),
                INACTIVE = 0x00,
                ACTIVE = 0x80,
            ),
            BitStruct("beginning",
                Octet("head"),
                Bits("sect", 6),
                Bits("cyl", 10),
            ),
            Enum(UBInt8("type"),
                Nothing = 0x00,
                FAT12 = 0x01,
                XENIX_ROOT = 0x02,
                XENIX_USR = 0x03,
                FAT16_old = 0x04,
                Extended_DOS = 0x05,
                FAT16 = 0x06,
                FAT32 = 0x0b,
                FAT32_LBA = 0x0c,
                NTFS = 0x07,
                LINUX_SWAP = 0x82,
                LINUX_NATIVE = 0x83,
                _default_ = Pass,
            ),
            BitStruct("ending",
                Octet("head"),
                Bits("sect", 6),
                Bits("cyl", 10),
            ),
            UBInt32("sector_offset"), # offset from MBR in sectors
            UBInt32("size"), # in sectors
        )
    ),
    Const("signature", b"\x55\xAA"),
)

There's a TCP/IP stack example that really shows how powerful the construct model is, with the ability to have bite-sized blocks of definitions that you combine into a single parser/generator.

I know there are PEG / EBNF parser generators, but I was hoping for something a little prettier to look at.

This isn't the same as Python's Construct package, but there is a version of Yacc for Go:

Yacc's grammar is similar to EBNF, so it may not meet your criteria, but it's widely used and understood, so I think it's worth mentioning.