Skip to content

How to Base64 Encode/Decode in Golang

Go ships with an encoding/base64 package that allows for encode and decoding of Base64 strings.

Import the base64 package and then start using it!

Base64 encode/decode a string

package main import ( b64 "encoding/base64" "fmt" ) func main() { // define a string data := "This is a test string!" // Encode to Base64 sEnc := b64.StdEncoding.EncodeToString([]byte(data)) fmt.Println(sEnc) // Decode from Base64 sDec, _ := b64.StdEncoding.DecodeString(sEnc) fmt.Println(string(sDec)) // URL Encode uEnc := b64.URLEncoding.EncodeToString([]byte(data)) fmt.Println(uEnc) // URL Decode uDec, _ := b64.URLEncoding.DecodeString(uEnc) fmt.Println(string(uDec)) }
Code language: Go (go)

See also  Calculate the Most Frequent Weekdays in Go
Tags:
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x