Use TLB Loader for struct serialization
masterInstead of manual cell assembly using cell.BeginCell(), use TLB tags to map Go structs directly to TON cells. This is more error-prone to do manually and TLB is the recommended way for application-level code.
Common TLB Tags:
tlb:"#hex": Exact hex magic/constructor.tlb:"## N": Fixed-width integer ofNbits.tlb:"addr": TON address (*address.Address).tlb:"^": Referenced cell (nested value in a different cell).tlb:".": Nested struct in the current cell.tlb:"maybe <tag>": Optional value (presence bit + value).tlb:"either X Y": Selector bit (0 for X, 1 for Y).tlb:"dict N": Dictionary withN-bit keys.tlb:"?FieldName <tag>": Conditional field based on a previously declared booleanFieldName.
Use tlb.Parse(&struct, cell) to decode and tlb.ToCell(struct) to encode.
type ExamplePayload struct {
_ tlb.Magic `tlb:"#a1b2c3d4"` // Magic prefix
QueryID uint64 `tlb:"## 64"` // 64-bit uint
Flags uint8 `tlb:"## 8"` // 8-bit uint
Body *cell.Cell `tlb:"^"` // Referenced cell
}
// Parsing
var payload ExamplePayload
if err := tlb.Parse(&payload, payloadCell); err != nil {
panic(err)
}
// Serializing
payloadCell, err := tlb.ToCell(payload)