### Install gamf Library Source: https://github.com/gnolizuh/gamf/blob/main/README.md Use 'go get' to install the gamf library. Ensure you have a Go environment set up. ```bash go get github.com/gnolizuh/gamf ``` -------------------------------- ### AMF0 Float Marshal and Unmarshal Source: https://github.com/gnolizuh/gamf/blob/main/README.md Example of marshaling a float64 to AMF0 and unmarshaling it. Useful for AMF0 data streams. ```go in := 1.0 bs, _ := Marshal(&in) var out float64 Unmarshal(bs, &out) ``` -------------------------------- ### AMF0 Struct to Struct Marshal and Unmarshal Source: https://github.com/gnolizuh/gamf/blob/main/README.md Example of marshaling a struct to AMF0 and unmarshaling it back into a struct. Uses `amf` tags for field mapping. ```go type Struct struct { Int int `amf:"tag_int" String string `amf:"tag_string" Bool bool `amf:"tag_bool" Object struct { Int int `amf:"tag_int" String string `amf:"tag_string" Bool bool `amf:"tag_bool" } `amf:"tag_object" } in := Struct{} // with value be initialized bs, _ := Marshal(&in) out := Struct{} Unmarshal(bs, &out) ``` -------------------------------- ### AMF0 Map Marshal and Unmarshal Source: https://github.com/gnolizuh/gamf/blob/main/README.md Example of marshaling a map to AMF0 format and unmarshaling it. Supports common map[string]any types. ```go in := map[string]any{"Int": 1.0, "String": "1", "Bool": true} bs, _ := Marshal(&in) out := make(map[string]any) Unmarshal(bs, &out) ``` -------------------------------- ### AMF0 String Marshal and Unmarshal Source: https://github.com/gnolizuh/gamf/blob/main/README.md Demonstrates marshaling a string to AMF0 format and then unmarshaling it. Standard AMF0 string handling. ```go in := "1" bs, _ := Marshal(&in) var out string Unmarshal(bs, &out) ``` -------------------------------- ### AMF0 Integer Marshal and Unmarshal Source: https://github.com/gnolizuh/gamf/blob/main/README.md Demonstrates marshaling an integer to AMF0 format and then unmarshaling it back. This is for AMF0 compatibility. ```go in := 1 bs, _ := Marshal(&in) var out int Unmarshal(bs, &out) ``` -------------------------------- ### AMF0 Struct to Map Marshal and Unmarshal Source: https://github.com/gnolizuh/gamf/blob/main/README.md Demonstrates marshaling a struct to AMF0 format and unmarshaling it into a map. Useful when the target structure is dynamic. ```go type Struct struct { Int int `amf:"tag_int" String string `amf:"tag_string" Bool bool `amf:"tag_bool" Object struct { Int int `amf:"tag_int" String string `amf:"tag_string" Bool bool `amf:"tag_bool" } `amf:"tag_object" } in := Struct{} // with value be initialized bs, _ := Marshal(&in) out := make(map[string]any) Unmarshal(bs, &out) ``` -------------------------------- ### AMF3 Integer Marshal and Unmarshal Source: https://github.com/gnolizuh/gamf/blob/main/README.md Shows how to encode an integer using AMF3 and decode it back. Uses Encoder and Decoder with AMF3 version. ```go var bs []byte buf := bytes.NewBuffer(bs) in := 1 NewEncoder().WithWriter(buf).Encode(&in) var out int NewDecoder().WithReader(buf).Decode(&out) ``` -------------------------------- ### AMF3 String Marshal and Unmarshal Source: https://github.com/gnolizuh/gamf/blob/main/README.md Shows encoding a string using AMF3 and decoding it. Requires specifying AMF3 version for the encoder/decoder. ```go var bs []byte buf := bytes.NewBuffer(bs) in := "1" NewEncoder().WithWriter(buf).WithVersion(Version3).Encode(&in) var out string NewDecoder().WithReader(buf).WithVersion(Version3).Decode(&out) ``` -------------------------------- ### AMF0 Slice Marshal and Unmarshal Source: https://github.com/gnolizuh/gamf/blob/main/README.md Demonstrates marshaling a slice of integers to AMF0 format and then unmarshaling it. Handles basic slice types. ```go in := []int{1, 2, 3} bs, _ := Marshal(&in) var out []int Unmarshal(bs, &out) ``` -------------------------------- ### AMF3 Bool Marshal and Unmarshal Source: https://github.com/gnolizuh/gamf/blob/main/README.md Illustrates encoding a boolean using AMF3 and decoding it. Sets the version to AMF3 for accurate encoding/decoding. ```go var bs []byte buf := bytes.NewBuffer(bs) in := true NewEncoder().WithWriter(buf).WithVersion(Version3).Encode(&in) out := false NewDecoder().WithReader(buf).WithVersion(Version3).Decode(&out) ``` -------------------------------- ### AMF3 Float Marshal and Unmarshal Source: https://github.com/gnolizuh/gamf/blob/main/README.md Illustrates encoding a float using AMF3 and decoding it. Explicitly sets the version to AMF3. ```go var bs []byte buf := bytes.NewBuffer(bs) in := 1 NewEncoder().WithWriter(buf).WithVersion(Version3).Encode(&in) var out int NewDecoder().WithReader(buf).WithVersion(Version3).Decode(&out) ``` -------------------------------- ### Marshal Go Values to AMF0 Source: https://context7.com/gnolizuh/gamf/llms.txt Encodes Go values into AMF0 binary format. Accepts variadic arguments for encoding multiple values sequentially. Ensure values are passed as pointers. ```go package main import ( "fmt" "github.com/gnolizuh/gamf" ) func main() { // Marshal an integer intVal := 42 data, err := amf.Marshal(&intVal) if err != nil { panic(err) } fmt.Printf("Integer encoded: %d bytes\n", len(data)) // Marshal a string strVal := "hello world" data, err = amf.Marshal(&strVal) if err != nil { panic(err) } fmt.Printf("String encoded: %d bytes\n", len(data)) // Marshal multiple values at once num := 3.14 text := "pi" flag := true data, err = amf.Marshal(&num, &text, &flag) if err != nil { panic(err) } fmt.Printf("Multiple values encoded: %d bytes\n", len(data)) } ``` -------------------------------- ### Configure AMF Version Constants in Go Source: https://context7.com/gnolizuh/gamf/llms.txt Use version constants to specify AMF0 or AMF3 encoding formats when using encoders. ```go package main import ( "bytes" "fmt" "github.com/gnolizuh/gamf" ) func main() { // Version constants fmt.Printf("AMF0 Version: %d\n", amf.Version0) // Output: AMF0 Version: 0 fmt.Printf("AMF3 Version: %d\n", amf.Version3) // Output: AMF3 Version: 3 // Using versions with encoder/decoder var buf bytes.Buffer // AMF0 (default) - numbers as 8-byte doubles amf.NewEncoder().WithWriter(&buf).WithVersion(amf.Version0).Encode(42) fmt.Printf("AMF0 integer size: %d bytes\n", buf.Len()) // 9 bytes (1 marker + 8 double) buf.Reset() // AMF3 - integers as variable-length U29 amf.NewEncoder().WithWriter(&buf).WithVersion(amf.Version3).Encode(42) fmt.Printf("AMF3 integer size: %d bytes\n", buf.Len()) // 2 bytes (1 marker + 1 U29) } ``` -------------------------------- ### Slice and Array Serialization (AMF0 and AMF3) Source: https://context7.com/gnolizuh/gamf/llms.txt Encodes Go slices and arrays as AMF strict arrays. Demonstrates AMF0 for integer slices and AMF3 for mixed-type slices using explicit encoder/decoder configuration. ```go package main import ( "bytes" "fmt" "github.com/gnolizuh/gamf" ) func main() { // Integer slice (AMF0) intSlice := []int{1, 2, 3, 4, 5} data, _ := amf.Marshal(&intSlice) var decoded []int amf.Unmarshal(data, &decoded) fmt.Printf("Integer slice: %v\n", decoded) // Mixed type slice with AMF3 var buf bytes.Buffer encoder := amf.NewEncoder(). WithWriter(&buf). WithVersion(amf.Version3) mixed := []any{ 1.0, "text", true, map[string]any{"key": "value"}, } encoder.Encode(&mixed) reader := bytes.NewReader(buf.Bytes()) decoder := amf.NewDecoder(). WithReader(reader). WithVersion(amf.Version3) output := []any{0.0, "", false, map[string]any{}} decoder.Decode(&output) fmt.Printf("Mixed slice: %v\n", output) // Output: Mixed slice: [1 text true map[key:value]] } ``` -------------------------------- ### Implement Custom Marshaler Interface in Go Source: https://context7.com/gnolizuh/gamf/llms.txt Define custom AMF encoding logic for complex types by implementing the amf.Marshaler interface. ```go package main import ( "fmt" "github.com/gnolizuh/gamf" ) type Point struct { X, Y float64 } // MarshalAMF implements the amf.Marshaler interface func (p Point) MarshalAMF() ([]byte, error) { // Custom encoding: encode as a simple map data := map[string]any{ "x": p.X, "y": p.Y, } return amf.Marshal(&data) } func main() { point := Point{X: 10.5, Y: 20.3} data, err := amf.Marshal(&point) if err != nil { panic(err) } fmt.Printf("Custom encoded point: %d bytes\n", len(data)) // Decode to map result := make(map[string]any) amf.Unmarshal(data, &result) fmt.Printf("Decoded point: x=%v, y=%v\n", result["x"], result["y"]) // Output: Decoded point: x=10.5, y=20.3 } ``` -------------------------------- ### Marshal Function Source: https://context7.com/gnolizuh/gamf/llms.txt Encodes Go values into AMF0 binary format. ```APIDOC ## Marshal ### Description Encodes Go values into AMF0 binary format. Accepts variadic arguments allowing multiple values to be encoded in sequence. ### Parameters - **v** (any...) - Required - Variadic list of pointers to values to be encoded. ### Request Example ```go intVal := 42 data, err := amf.Marshal(&intVal) ``` ### Response - **data** ([]byte) - The resulting AMF0 encoded byte slice. - **err** (error) - Error object if encoding fails. ``` -------------------------------- ### Struct Serialization with Custom AMF Tags Source: https://context7.com/gnolizuh/gamf/llms.txt Serializes Go structs to AMF format using custom field names defined in `amf` tags. Supports `omitempty` for omitting empty fields. ```go package main import ( "fmt" "github.com/gnolizuh/gamf" ) type User struct { ID int `amf:"user_id"` Username string `amf:"username"` Email string `amf:"email,omitempty"` Active bool `amf:"is_active"` Profile struct { Bio string `amf:"bio"` Website string `amf:"website,omitempty"` } `amf:"profile"` } func main() { user := User{ ID: 123, Username: "johndoe", Email: "john@example.com", Active: true, } user.Profile.Bio = "Software developer" // Encode struct to AMF data, err := amf.Marshal(&user) if err != nil { panic(err) } fmt.Printf("Encoded user: %d bytes\n", len(data)) // Decode back to struct var decoded User err = amf.Unmarshal(data, &decoded) if err != nil { panic(err) } fmt.Printf("Decoded user: ID=%d, Username=%s, Active=%v\n", decoded.ID, decoded.Username, decoded.Active) // Output: Decoded user: ID=123, Username=johndoe, Active=true } ``` -------------------------------- ### Streaming AMF0/AMF3 Decoder Source: https://context7.com/gnolizuh/gamf/llms.txt Creates a streaming decoder that reads AMF values from an io.Reader. Supports both AMF0 and AMF3 decoding. Use WithReader to specify the input source and WithVersion for the protocol. ```go package main import ( "bytes" "fmt" "github.com/gnolizuh/gamf" ) func main() { // Encode some data first var buf bytes.Buffer encoder := amf.NewEncoder(). WithWriter(&buf). WithVersion(amf.Version3) input := map[string]any{ "id": 1.0, "name": "test", "values": []any{1.0, 2.0, 3.0}, } encoder.Encode(&input) // Decode using streaming decoder reader := bytes.NewReader(buf.Bytes()) decoder := amf.NewDecoder(). WithReader(reader). WithVersion(amf.Version3) var output map[string]any err := decoder.Decode(&output) if err != nil { panic(err) } fmt.Printf("Decoded map: %+v\n", output) // Output: Decoded map: map[id:1 name:test values:[1 2 3]] } ``` -------------------------------- ### AMF3 Slice Marshal and Unmarshal Source: https://github.com/gnolizuh/gamf/blob/main/README.md Shows encoding a slice containing mixed types (float, string, bool, map) using AMF3 and decoding it. Requires explicit AMF3 version. ```go var bs []byte buf := bytes.NewBuffer(bs) in := []any{1.0, "1", true, map[string]any{"Int": 1.0, "String": "1", "Bool": true}} NewEncoder().WithWriter(buf).WithVersion(Version3).Encode(&in) out := []any{0.0, "0", false, map[string]any{}} NewDecoder().WithReader(buf).WithVersion(Version3).Decode(&out) ``` -------------------------------- ### Streaming AMF0/AMF3 Encoder Source: https://context7.com/gnolizuh/gamf/llms.txt Creates a streaming encoder that writes AMF values to an io.Writer. Supports AMF0 (default) and AMF3 via WithVersion method. Use WithWriter to specify the output destination. ```go package main import ( "bytes" "fmt" "github.com/gnolizuh/gamf" ) func main() { var buf bytes.Buffer // AMF0 encoding (default) encoder := amf.NewEncoder().WithWriter(&buf) value := map[string]any{ "name": "John", "age": 30, "active": true, } err := encoder.Encode(&value) if err != nil { panic(err) } fmt.Printf("AMF0 encoded %d bytes\n", buf.Len()) // AMF3 encoding with version setting buf.Reset() encoder3 := amf.NewEncoder(). WithWriter(&buf). WithVersion(amf.Version3) data := []any{1.0, "hello", true} err = encoder3.Encode(&data) if err != nil { panic(err) } fmt.Printf("AMF3 encoded %d bytes\n", buf.Len()) } ``` -------------------------------- ### Map Serialization and Deserialization Source: https://context7.com/gnolizuh/gamf/llms.txt Directly serializes and deserializes Go maps with string keys into AMF objects. Supports nested maps and slices within maps. ```go package main import ( "fmt" "github.com/gnolizuh/gamf" ) func main() { // Simple map data := map[string]any{ "Int": 1.0, "String": "hello", "Bool": true, } encoded, err := amf.Marshal(&data) if err != nil { panic(err) } fmt.Printf("Encoded map: %d bytes\n", len(encoded)) // Decode back result := make(map[string]any) err = amf.Unmarshal(encoded, &result) if err != nil { panic(err) } fmt.Printf("Decoded: Int=%v, String=%v, Bool=%v\n", result["Int"], result["String"], result["Bool"]) // Output: Decoded: Int=1, String=hello, Bool=true // Nested map nested := map[string]any{ "user": map[string]any{ "name": "Alice", "age": 25.0, }, "items": []any{"a", "b", "c"}, } encoded, _ = amf.Marshal(&nested) output := make(map[string]any) amf.Unmarshal(encoded, &output) fmt.Printf("Nested user: %v\n", output["user"]) } ``` -------------------------------- ### Implement Custom Unmarshaler Interface in Go Source: https://context7.com/gnolizuh/gamf/llms.txt Define custom AMF decoding logic for complex types by implementing the amf.Unmarshaler interface. ```go package main import ( "fmt" "github.com/gnolizuh/gamf" ) type Config struct { data map[string]any } // UnmarshalAMF implements the amf.Unmarshaler interface func (c *Config) UnmarshalAMF(data []byte) error { c.data = make(map[string]any) return amf.Unmarshal(data, &c.data) } func (c *Config) Get(key string) any { return c.data[key] } func main() { // Encode a map original := map[string]any{ "setting1": "value1", "setting2": 42.0, "enabled": true, } data, _ := amf.Marshal(&original) // Decode using custom Unmarshaler var config Config err := amf.Unmarshal(data, &config) if err != nil { panic(err) } fmt.Printf("Setting1: %v\n", config.Get("setting1")) // Output: Setting1: value1 fmt.Printf("Setting2: %v\n", config.Get("setting2")) // Output: Setting2: 42 fmt.Printf("Enabled: %v\n", config.Get("enabled")) // Output: Enabled: true } ``` -------------------------------- ### Unmarshal Function Source: https://context7.com/gnolizuh/gamf/llms.txt Decodes AMF0 binary data into Go values. ```APIDOC ## Unmarshal ### Description Decodes AMF0 binary data into Go values. Accepts variadic pointer arguments for decoding multiple sequential values. ### Parameters - **data** ([]byte) - Required - The AMF0 binary data to decode. - **v** (any...) - Required - Variadic list of pointers where decoded values will be stored. ### Request Example ```go var output int err := amf.Unmarshal(data, &output) ``` ### Response - **err** (error) - Error object if decoding fails. ``` -------------------------------- ### Unmarshal AMF0 Data into Go Values Source: https://context7.com/gnolizuh/gamf/llms.txt Decodes AMF0 binary data into Go values. Accepts variadic pointer arguments for decoding multiple sequential values. Ensure destination variables are pointers. ```go package main import ( "fmt" "github.com/gnolizuh/gamf" ) func main() { // Encode then decode an integer input := 42 data, _ := amf.Marshal(&input) var output int err := amf.Unmarshal(data, &output) if err != nil { panic(err) } fmt.Printf("Decoded integer: %d\n", output) // Output: Decoded integer: 42 // Decode a float floatIn := 3.14159 data, _ = amf.Marshal(&floatIn) var floatOut float64 err = amf.Unmarshal(data, &floatOut) if err != nil { panic(err) } fmt.Printf("Decoded float: %f\n", floatOut) // Output: Decoded float: 3.141590 // Decode multiple values a, b := 100, "test" data, _ = amf.Marshal(&a, &b) var outA int var outB string err = amf.Unmarshal(data, &outA, &outB) if err != nil { panic(err) } fmt.Printf("Decoded: %d, %s\n", outA, outB) // Output: Decoded: 100, test } ``` -------------------------------- ### Struct to Map Decoding for Dynamic Handling Source: https://context7.com/gnolizuh/gamf/llms.txt Decodes AMF objects, originally encoded from structs, into maps for dynamic data access. Useful when the exact struct type is not known at compile time. ```go package main import ( "fmt" "github.com/gnolizuh/gamf" ) type Config struct { Host string `amf:"host"` Port int `amf:"port"` Secure bool `amf:"secure"` Settings struct { Timeout int `amf:"timeout"` Retry int `amf:"retry"` } `amf:"settings"` } func main() { config := Config{ Host: "localhost", Port: 8080, Secure: true, } config.Settings.Timeout = 30 config.Settings.Retry = 3 // Encode struct data, err := amf.Marshal(&config) if err != nil { panic(err) } // Decode to map for dynamic access result := make(map[string]any) err = amf.Unmarshal(data, &result) if err != nil { panic(err) } fmt.Printf("Host: %v\n", result["host"]) fmt.Printf("Port: %v\n", result["port"]) fmt.Printf("Settings: %v\n", result["settings"]) } ``` -------------------------------- ### Decoder Type Source: https://context7.com/gnolizuh/gamf/llms.txt Streaming decoder that reads AMF values from an io.Reader. ```APIDOC ## Decoder ### Description Streaming decoder that reads AMF values from a Reader interface. Supports both AMF0 and AMF3 decoding. ### Methods - **WithReader(io.Reader)** - Sets the input source. - **WithVersion(int)** - Sets the AMF version to decode. - **Decode(any)** - Decodes the next value from the reader into the provided pointer. ### Request Example ```go decoder := amf.NewDecoder().WithReader(reader).WithVersion(amf.Version3) err := decoder.Decode(&output) ``` ``` -------------------------------- ### Encoder Type Source: https://context7.com/gnolizuh/gamf/llms.txt Streaming encoder that writes AMF values to an io.Writer. ```APIDOC ## Encoder ### Description Streaming encoder that writes AMF values to an io.Writer. Supports both AMF0 (default) and AMF3 versions through method chaining. ### Methods - **WithWriter(io.Writer)** - Sets the output destination. - **WithVersion(int)** - Sets the AMF version (e.g., amf.Version3). - **Encode(any)** - Encodes the provided value to the writer. ### Request Example ```go encoder := amf.NewEncoder().WithWriter(&buf).WithVersion(amf.Version3) err := encoder.Encode(&value) ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.