To convert a string representation of a base64-encoded value to its decoded form using strconv
package in Golang, you need to use the DecodeString
function from the encoding/base64
package.
Here's an example of how to do it:
package main
import (
"encoding/base64"
"fmt"
"log"
)
func main() {
encodedString := "SGVsbG8gV29ybGQh"
// Decoding the base64-encoded string
decodedBytes, err := base64.StdEncoding.DecodeString(encodedString)
if err != nil {
log.Fatal("Error decoding base64 string:", err)
}
// Converting the decoded bytes to string
decodedString := string(decodedBytes)
fmt.Println(decodedString) // Output: "Hello World!"
}
In this example, the base64.StdEncoding.DecodeString
function is used to decode the encodedString
value. If there's an error during decoding, it will be logged using log.Fatal
. The decodedBytes
are then converted to a string using the string
function, resulting in the decoded value "Hello World!"
.