To encode data in hexadecimal and base64 encoding in Go, you can use the encoding/hex
and encoding/base64
packages respectively. Here's an example for encoding data in hexadecimal and base64:
package main
import (
"encoding/hex"
"fmt"
)
func main() {
data := []byte("Hello, World!")
encodedStr := hex.EncodeToString(data)
fmt.Println(encodedStr) // Output: 48656c6c6f2c20576f726c6421
}
package main
import (
"encoding/base64"
"fmt"
)
func main() {
data := []byte("Hello, World!")
encodedStr := base64.StdEncoding.EncodeToString(data)
fmt.Println(encodedStr) // Output: SGVsbG8sIFdvcmxkIQ==
}
In both cases, you need to pass the data you want to encode (in the form of a byte slice) to the corresponding EncodeToString()
function. The result is a string representing the encoded data.