Wednesday, February 21, 2024
HomeSoftware EngineeringHow you can Base64 Encode/Decode in Golang

How you can Base64 Encode/Decode in Golang


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

Import the base64 bundle after which begin utilizing it!

Base64 encode/decode a string

bundle major

import (
	b64 "encoding/base64"
	"fmt"
)

func major() {
	// outline a string
	information := "This can be a take a look at string!"

	// Encode to Base64
	sEnc := b64.StdEncoding.EncodeToString([]byte(information))
	fmt.Println(sEnc)

	// Decode from Base64
	sDec, _ := b64.StdEncoding.DecodeString(sEnc)
	fmt.Println(string(sDec))

	// URL Encode
	uEnc := b64.URLEncoding.EncodeToString([]byte(information))
	fmt.Println(uEnc)

	// URL Decode
	uDec, _ := b64.URLEncoding.DecodeString(uEnc)
	fmt.Println(string(uDec))
}
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments