Supplement document of nbt API
This commit is contained in:
@ -5,35 +5,75 @@ import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
//goland:noinspection SpellCheckingInspection
|
||||
func ExampleUnmarshal() {
|
||||
var data = []byte{
|
||||
0x08, 0x00, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x00, 0x09,
|
||||
0x42, 0x61, 0x6e, 0x61, 0x6e, 0x72, 0x61, 0x6d, 0x61,
|
||||
func ExampleDecoder_Decode() {
|
||||
reader := bytes.NewReader([]byte{
|
||||
0x0a, // Start TagCompound("")
|
||||
0x00, 0x00,
|
||||
|
||||
0x08, // TagString("Author"): "Tnze"
|
||||
0x00, 0x06, 'A', 'u', 't', 'h', 'o', 'r',
|
||||
0x00, 0x04, 'T', 'n', 'z', 'e',
|
||||
|
||||
0x00, // End TagCompound
|
||||
})
|
||||
|
||||
var value struct {
|
||||
Author string
|
||||
}
|
||||
|
||||
var Name string
|
||||
|
||||
if err := Unmarshal(data, &Name); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Println(Name)
|
||||
|
||||
// Output: Bananrama
|
||||
}
|
||||
|
||||
func ExampleMarshal() {
|
||||
var value = struct {
|
||||
Name string `nbt:"name"`
|
||||
}{"Tnze"}
|
||||
|
||||
data, err := Marshal(value)
|
||||
decoder := NewDecoder(reader)
|
||||
_, err := decoder.Decode(&value)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Printf("% 02x ", data)
|
||||
fmt.Println(value)
|
||||
|
||||
// Output:
|
||||
// {Tnze}
|
||||
}
|
||||
|
||||
func ExampleDecoder_Decode_singleTagString() {
|
||||
reader := bytes.NewReader([]byte{
|
||||
// TagString
|
||||
0x08,
|
||||
// TagName
|
||||
0x00, 0x04,
|
||||
0x6e, 0x61, 0x6d, 0x65,
|
||||
// Content
|
||||
0x00, 0x09,
|
||||
0x42, 0x61, 0x6e, 0x61, 0x6e, 0x72, 0x61, 0x6d, 0x61,
|
||||
})
|
||||
|
||||
var Name string
|
||||
|
||||
decoder := NewDecoder(reader)
|
||||
tagName, err := decoder.Decode(&Name)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Println(tagName)
|
||||
fmt.Println(Name)
|
||||
|
||||
// Output:
|
||||
// name
|
||||
// Bananrama
|
||||
}
|
||||
|
||||
func ExampleEncoder_Encode_tagCompound() {
|
||||
var value = struct {
|
||||
Name string `nbt:"name"`
|
||||
}{"Tnze"}
|
||||
|
||||
var buff bytes.Buffer
|
||||
encoder := NewEncoder(&buff)
|
||||
err := encoder.Encode(value, "")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Printf("% 02x ", buff.Bytes())
|
||||
|
||||
// Output:
|
||||
// 0a 00 00 08 00 04 6e 61 6d 65 00 04 54 6e 7a 65 00
|
||||
|
Reference in New Issue
Block a user