encoding/asn1

Guided tour · Encoding · pkg.go.dev →

ASN.1 DER encoding — the format certificates, private keys, and X.509 structures live in. Reach for this when interoperating with PKI.

Marshal / Unmarshal

Encode a simple struct

type Point struct{ X, Y int }
b, _ := asn1.Marshal(Point{3, 4})
fmt.Printf("% x\n", b)

var p Point
asn1.Unmarshal(b, &p)
fmt.Println(p)

Struct tags for tagged fields

type Cert struct {
    Version    int      `asn1:"optional,explicit,default:1,tag:0"`
    Subject    string
}