minecraft/nbt package. NBT is Minecraft’s binary data format used for world data, entities, items, and more.
Overview
Theminecraft/nbt package provides:
- Encoding Go structs/maps to NBT binary format
- Decoding NBT binary data to Go structs/maps
- Multiple encoding variants (NetworkLittleEndian, NetworkBigEndian, JavaEdition)
- Streaming encoder/decoder for efficient processing
Type Mappings
Go Types to NBT Tags
| Go Type | NBT Tag |
|---|---|
byte/uint8 | TAG_Byte |
bool | TAG_Byte |
int16 | TAG_Short |
int32 | TAG_Int |
int64 | TAG_Long |
float32 | TAG_Float |
float64 | TAG_Double |
[...]byte | TAG_ByteArray |
[...]int32 | TAG_IntArray |
[...]int64 | TAG_LongArray |
string | TAG_String |
[]<type> | TAG_List |
struct{...} | TAG_Compound |
map[string]<type> | TAG_Compound |
Basic Encoding
Basic Decoding
Struct Tags
Field Naming
Usenbt tags to control field names:
Omit Empty
Skip zero-value fields:Ignore Fields
Exclude fields from encoding/decoding:Encoding Variants
Network Little Endian (Default)
Used for Bedrock Edition network protocol:Network Big Endian
Used for Java Edition network protocol:File Encoding
For reading/writing world files:Streaming API
Encoder
For writing to streams:Decoder
For reading from streams:Custom Encoding
Working with Maps
Dynamic Data
Use maps for unknown structure:Encoding Maps
Array Types
Byte Arrays
Int Arrays
Long Arrays
Lists
Homogeneous Lists
Lists must contain elements of the same type:Empty Lists
Complete Examples
World Entity
Player Inventory
Decode Unknown Structure
Error Handling
Encoding Errors
Decoding Errors
Best Practices
- Use Structs: Prefer structs over maps for known structures
- Tag Fields: Use
nbttags to match Minecraft’s NBT format - Handle Errors: Always check encoding/decoding errors
- Use Correct Encoding: Use
NetworkLittleEndianfor Bedrock network data - Validate Data: Validate decoded data before using it
Performance Tips
- Stream Large Data: Use
Encoder/Decoderfor large files - Reuse Buffers: The package uses buffer pools internally
- Avoid Deep Nesting: Deep NBT structures are slower to process
Next Steps
- Learn about packet encoding
- Understand world data formats
- Build tools to inspect NBT data