How to decode hexadecimal or base64-encoded data in Go?

To decode hexadecimal or base64-encoded data in Go, you can use the built-in encoding packages provided by Go.

Here's an example of how to decode hexadecimal-encoded data using the encoding/hex package:

package main import ( "encoding/hex" "fmt" ) func main() { // Hexadecimal-encoded data hexData := "48656c6c6f20576f726c64" // Decode hex data decodedData, err := hex.DecodeString(hexData) if err != nil { fmt.Println("Error decoding hex data:", err) return } // Print the decoded data as a string fmt.Println("Decoded data:", string(decodedData)) }

And here's an example of how to decode base64-encoded data using the encoding/base64 package:

package main import ( "encoding/base64" "fmt" ) func main() { // Base64-encoded data base64Data := "SGVsbG8gV29ybGQ=" // Decode base64 data decodedData, err := base64.StdEncoding.DecodeString(base64Data) if err != nil { fmt.Println("Error decoding base64 data:", err) return } // Print the decoded data as a string fmt.Println("Decoded data:", string(decodedData)) }

You can replace the hexData or base64Data variables in the examples with your own encoded data. The hex.DecodeString and base64.StdEncoding.DecodeString functions are used to decode the encoded data, and the result is stored in the decodedData variable.