🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this tutorial, we will learn how to use base64.StdEncoding and base64.URLEncoding to encode and decode a string in Golang.
Golang provides built-in support for base64 encoding/decoding in the package encoding/base64.
The types of encoding available are:- StdEncoding: standard base64 encoding
- URLEncoding: alternate base64 encoding used in URLs and file names
- RawStdEncoding: standard, raw, unpadded base64 encoding
- RawURLEncoding: unpadded alternate base64 encoding for URLs and file names
Golang Encoding and Decoding Example using base64.StdEncoding
Here is an example of encoding and then decoding the string, “source code examples” using base64 encoding with StdEncoding.
package main
import (
"encoding/base64"
"fmt"
)
func main() {
// String to encode
str := "Source code examples"
// base64.StdEncoding: Standard encoding with padding
// It requires a byte slice so we cast the string to []byte
encodedStr := base64.StdEncoding.EncodeToString([]byte(str))
fmt.Println("Encoded:", encodedStr)
// Decoding may return an error, in case the input is not well formed
decodedStr, err := base64.StdEncoding.DecodeString(encodedStr)
if err != nil {
panic("malformed input")
}
fmt.Println("Decoded:", string(decodedStr))
}
Output:
Encoded: U291cmNlIGNvZGUgZXhhbXBsZXM= Decoded: Source code examples
Golang Encode and Decode Example using base64.URLEncoding
Here is an example of encoding and then decoding the URL using base64 encoding with URLEncoding.
package main
import (
"encoding/base64"
"fmt"
)
func main() {
// String to encode
str := "https://www.sourcecodeexamples.net/p/golang-source-code-examples.html"
// It requires a byte slice so we cast the string to []byte
encodedStr := base64.URLEncoding.EncodeToString([]byte(str))
fmt.Println("Encoded:", encodedStr)
// Decoding may return an error, in case the input is not well formed
decodedStr, err := base64.URLEncoding.DecodeString(encodedStr )
if err != nil {
panic("malformed input")
}
fmt.Println("Decoded:", string(decodedStr))
}
Output:
Encoded: aHR0cHM6Ly93d3cuc291cmNlY29kZWV4YW1wbGVzLm5ldC9wL2dvbGFuZy1zb3VyY2UtY29kZS1leGFtcGxlcy5odG1s Decoded: https://www.sourcecodeexamples.net/p/golang-source-code-examples.html
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment