Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ToBytes ¶
ToBytes converts a given value into a byte slice using the specified type. It uses the ToBytesWithErr function to perform the type conversion and decompression but unlike ToBytesWithErr, this function will panic if an error occurred during the process.
Parameters:
- t: The type of decompression to use, defined by a Type value (TypeGzip, TypeGzipBase64, TypeDeflate, TypeDeflateBase64).
- a: The interface value to be converted to a byte slice.
Returns:
- []byte: A byte slice which is the result of decompressing the input value.
Example:
var data = "hello world" // Using TypeGzip bs := ToBytes(TypeGzip, data) fmt.Println("Decompressed data (gzip): ", string(bs)) // Using TypeGzipBase64 bs = ToBytes(TypeGzipBase64, data) fmt.Println("Decompressed data (gzip base64): ", string(bs))
Panic: The function will panic if any error occurred during the operation. For instance, if used with an unsupported Type value, it will cause a panic. Therefore, it's recommended to use it within a try catch if the types are not certain.
// This will cause a panic defer func() { if r := recover(); r != nil { fmt.Println("Recovered from ", r) } }() bs = ToBytes(Type("Unsupported"), data)
func ToBytesWithErr ¶
ToBytesWithErr converts a given value into a byte slice using the specified type. The function returns an error if the compression type is unsupported or if an error occurred during decompression.
Parameters:
- t: The type of decompression to use, defined by a Type values (TypeGzip, TypeGzipBase64, TypeDeflate, TypeDeflateBase64).
- a: The interface value to be converted to a byte slice.
Returns:
- []byte: A byte slice which is the result of decompressing the input value (if no error occurred)
- error: An error message detailing any error that occurred during the operation
Example usages:
// Using TypeGzip bs, err := ToBytesWithErr(TypeGzip, "compressed data as string") if err != nil { fmt.Println("Error while decompressing (gzip): ", err) } else { fmt.Println("Decompressed data (gzip): ", string(bs)) } // Using TypeGzipBase64 bs, err = ToBytesWithErr(TypeGzipBase64, "compressed data as base64 string") if err != nil { fmt.Println("Error while decompressing (gzip base64): ", err) } else { fmt.Println("Decompressed data (gzip base64): ", string(bs)) }
func ToString ¶
ToString is a convenient function that converts a given value into a string representation. It uses the ToStringWithErr function with the specified type to perform the conversion. If an error occurred, it panics, otherwise a string is returned. This is a useful function when you are confident that the input is in correct format.
Parameters:
- t: The type of decompression to be used. The type is defined by Type values (TypeGzip, TypeGzipBase64, TypeDeflate, TypeDeflateBase64).
- a: The interface value to be converted to a string.
Returns:
- string: The string representation of the given interface value. If there is no error during decompression and conversion.
Panic:
- Throws a panic if any error occurs during the conversion process.
Example:
var val = "compressed data as string" // Using TypeGzip fmt.Println(ToString(TypeGzip, val)) // "Decompressed data (gzip)" var valBase64 = "compressed data as base64 string" // Using TypeGzipBase64 fmt.Println(ToString(TypeGzipBase64, valBase64)) // "Decompressed data (gzip base64)"
func ToStringWithErr ¶
ToStringWithErr converts a given value into a string using the specified decompression type. The function relies on the ToBytesWithErr function to perform the operation, returning any errors that occur during the conversion process.
Parameters:
- t: The type of decompression to use, defined by Type values (TypeGzip, TypeGzipBase64, TypeDeflate, TypeDeflateBase64).
- a: The value to be converted into a string.
Returns:
- string: The string representation of the given value, if no errors occurred during the decompression and conversion process.
- error: An error message detailing any issues that occurred during the operation.
Example usages:
var compressedString = "compressed data as string" var compressedBase64String = "compressed data as base64 string" // Using TypeGzip str, err := ToStringWithErr(TypeGzip, compressedString) if err != nil { fmt.Println("Error while decompressing (gzip):", err) } else { fmt.Println("Decompressed and converted data (gzip):", str) } // Using TypeGzipBase64 str, err = ToStringWithErr(TypeGzipBase64, compressedBase64String) if err != nil { fmt.Println("Error while decompressing (gzip base64):", err) } else { fmt.Println("Decompressed and converted data (gzip base64):", str) }