是否可将.asn文件编译为Golang代码?是否存在规范的编译方案?
Absolutely—you can compile ASN.1 files into idiomatic Go code, and there are reliable, community-backed tools and workflows to make this happen, even considering Go's standard library only supports an ASN.1 subset. Here's a breakdown of your options:
1. Mature Community Code Generators
These tools parse your ASN.1 spec directly and generate Go code that integrates seamlessly with Go's standard library or provides standalone encoding/decoding logic:
asn1c with Go Backend
While originally built for C/C++,asn1csupports Go as an output target via the-gen-Goflag. It generates Go structs mapped to your ASN.1 types, complete with the necessaryasn1struct tags to work with Go'sencoding/asn1package. Run it like this:asn1c -gen-Go your_spec.asnThe output includes type definitions, helper functions for encoding/decoding, and handles common ASN.1 features like
SEQUENCE,SET,CHOICE, andENUMERATED.Go-Specific ASN.1 Generators
Tools likego-asn1-generatorare purpose-built for Go. They focus on generating clean, idiomatic Go code that aligns perfectly with theencoding/asn1package's expectations. They excel at handling ASN.1 constraints (likeOPTIONALorDEFAULT) and translating them into Go struct tags and optional fields.
2. Standard Library-First Workflow (For Simple Specs)
If your ASN.1 spec uses only the subset supported by Go's encoding/asn1 (BER/DER encoding, basic types, sequences, etc.), you can manually define Go structs with asn1 tags to match your ASN.1 types. For example:
ASN.1 snippet:
Person ::= SEQUENCE { name UTF8String, age INTEGER OPTIONAL }Corresponding Go struct:
type Person struct { Name string `asn1:"utf8"` Age int `asn1:"optional"` }
This works for small, simple specs, but for complex ASN.1 definitions, manual mapping is error-prone and inefficient—stick to code generators for those cases.
3. Recommended Standardized Workflow
Follow these steps for a reliable, repeatable process:
- Validate Your ASN.1 Spec: Ensure your
.asnfile has valid syntax (most generators will throw errors for invalid specs, so this is a quick first check). - Pick a Generator: Start with
asn1cif you're already familiar with it from C/Rust work, or a Go-specific tool if you want tighter integration with Go's ecosystem. - Generate & Customize: Run the generator with flags to set your output package name, adjust code formatting, or enable/disable specific features (like helper functions).
- Integrate & Test: Import the generated code into your project, and use either the generated helper functions or Go's
encoding/asn1package to test encoding/decoding with sample data.
Key Notes
- Different generators support different ASN.1 features—if your spec uses advanced constructs (like
AUTOMATIC TAGSor complex constraints), test the generator with your specific file first. - Generated Go code should follow Go's style guidelines; most tools let you run
go fmton the output automatically, or you can run it manually post-generation.
内容的提问来源于stack exchange,提问作者Bruno Ripa

