Documentation ¶
Overview ¶
Package typeutil contains a collection of type-related utility functions.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Decode ¶
Decode decodes a message encoded with the Encode function to the provided data object. The value underlying data must be a pointer to the correct type for the next data item received.
Example ¶
package main import ( "fmt" "log" "github.com/Vonage/gosrvlib/pkg/typeutil" ) func main() { type TestData struct { Alpha string Beta int } var data TestData msg := "Kf+BAwEBCFRlc3REYXRhAf+CAAECAQVBbHBoYQEMAAEEQmV0YQEEAAAAD/+CAQZhYmMxMjMB/gLtAA==" err := typeutil.Decode(msg, &data) if err != nil { log.Fatal(err) } fmt.Println(data) }
Output: {abc123 -375}
func Deserialize ¶ added in v1.69.0
Deserialize decodes a message encoded with the Serialize function to the provided data object. The value underlying data must be a pointer to the correct type for the next data item received.
Example ¶
package main import ( "fmt" "log" "github.com/Vonage/gosrvlib/pkg/typeutil" ) func main() { type TestData struct { Alpha string Beta int } var data TestData msg := "eyJBbHBoYSI6ImFiYzEyMyIsIkJldGEiOi0zNzV9Cg==" err := typeutil.Deserialize(msg, &data) if err != nil { log.Fatal(err) } fmt.Println(data) }
Output: {abc123 -375}
func Encode ¶
Encode encodes the input data to gob/base64 format into a string.
Example ¶
package main import ( "fmt" "log" "github.com/Vonage/gosrvlib/pkg/typeutil" ) func main() { type TestData struct { Alpha string Beta int } data := &TestData{Alpha: "test_string", Beta: -9876} v, err := typeutil.Encode(data) if err != nil { log.Fatal(err) } fmt.Println(v) }
Output:
func IsNil ¶
IsNil returns true if the input value is nil.
Example ¶
package main import ( "fmt" "github.com/Vonage/gosrvlib/pkg/typeutil" ) func main() { var nilChan chan int v := typeutil.IsNil(nilChan) fmt.Println(v) }
Output: true
func IsZero ¶
IsZero returns true if the input value is equal to the zero instance (e.g. empty string, 0 int, nil pointer).
Example ¶
package main import ( "fmt" "github.com/Vonage/gosrvlib/pkg/typeutil" ) func main() { var zeroInt int v := typeutil.IsZero(zeroInt) fmt.Println(v) }
Output: true
func Serialize ¶ added in v1.68.0
Serialize encodes the input data to JSON/base64 format into a string.
Example ¶
package main import ( "fmt" "log" "github.com/Vonage/gosrvlib/pkg/typeutil" ) func main() { type TestData struct { Alpha string Beta int } data := &TestData{Alpha: "test_string", Beta: -9876} v, err := typeutil.Serialize(data) if err != nil { log.Fatal(err) } fmt.Println(v) }
Output: eyJBbHBoYSI6InRlc3Rfc3RyaW5nIiwiQmV0YSI6LTk4NzZ9Cg==
Types ¶
This section is empty.