Example of Calling the Go Marshal Function

The following code snippet was derived from the golang/sample_ber/employee sample program in the ASN1C distribution:

package main

import (
	"employee/asn1gen"
	"encoding/hex"
	"fmt"
	"io/ioutil"
	"os"
)

func main() {
    var pdu asn1gen.PersonnelRecord

    // populate PDU type variable to be encoded
    fillName(&pdu.Name, "John", "P", "Smith")

    pdu.Title = "Director"
    pdu.Number = 51
    pdu.DateOfHire = "19710917"

    fillName(&pdu.NameOfSpouse, "Mary", "T", "Smith")

    // fill first child record

    var childInfo asn1gen.ChildInformation
    fillName(&childInfo.Name, "Ralph", "T", "Smith")
    childInfo.DateOfBirth = "19571111"
    pdu.Children = append(pdu.Children, childInfo)

    // fill second child record

    fillName(&childInfo.Name, "Susan", "B", "Jones")
    childInfo.DateOfBirth = "19590717"
    pdu.Children = append(pdu.Children, childInfo)

    // Marshal

    encodedData, err := asn1gen.Marshal(pdu)

    if err == nil {
        fmt.Printf("PersonnelRecord encoded successfully\n")
	fmt.Printf("%s\n", hex.Dump(encodedData))
	err = ioutil.WriteFile("message.per", encodedData, 0777)
    } else {
        fmt.Printf("Encoding failed: %s\n", err)
    }