How Marshal Works And Alternatives

The Marshal function is the most convenient way to encode, but taking a look at how it works also provides alternatives, should you want them.

First, we'll look at the other PER encode functions that are generated. There are two general forms:

   func PerEncode<type name>(pctxt *asn1rt.OSRTContext, value <go type>) (err error)
   
   func (pvalue *<type name>) PerEncode(pctxt *asn1rt.OSRTContext) (err error)
        

Here are two examples from the employee sample:

   func PerEncodeEmployeeNumber(pctxt *asn1rt.OSRTContext, value int64) (err error)
   
   func (pvalue *Name) PerEncode(pctxt *asn1rt.OSRTContext) (err error)
        

The first form is used when the Go type for the ASN.1 type is a built-in Go type - so basically it's used for the simple, nonconstructed ASN.1 types. The second form is used when the Go type is a generated type.

Now we can look at the Marshal function to see what it does and how these functions are used:

   // Create context object to manage encoding
   pctxt := new(asn1rt.OSRTContext)
   pctxt.InitEncode()   // Initialize for encoding
   pctxt.NewBitFieldList()   // Optional: enable bit tracing
   ...     
   err = v.PerEncode(pctxt)   // Invoke the encode function; v is a generated type
   ...
   pctxt.FlushBuffer()
   pctxt.PrintBitFieldList("pdu")   // Optional: print bit trace
        
   return pctxt.BufferData(), err   // access the encoded data