// Create a new NBT compound structure compound := nbt.NewCompound() // Add a string tag to the compound compound.Set("StringTag", nbt.NewString("Hello, Go NBT!")) // Add an int tag to the compound compound.Set("IntTag", nbt.NewInt(42)) // Write the compound to a file err := nbt.MarshalFile(compound, "mydata.nbt") if err != nil { log.Fatal(err) }
// Read an NBT compound from a file compound, err := nbt.UnmarshalFile("mydata.nbt") if err != nil { log.Fatal(err) } // Get the value of the StringTag stringVal, err := compound.GetString("StringTag") if err != nil { log.Fatal(err) } fmt.Println(stringVal) // Get the value of the IntTag intVal, err := compound.GetInt("IntTag") if err != nil { log.Fatal(err) } fmt.Println(intVal)In this example, we first read an NBT compound set from the `mydata.nbt` file using the `UnmarshalFile` function. We then retrieve the values of the `StringTag` and `IntTag` using the `GetString` and `GetInt` methods, respectively. Finally, we print out the values to the console. The package library used in these code examples is the `github.com/fortytw2/leaktest` package.