🎓 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
The fmt.Appendln function in Golang is part of the fmt package. It formats its arguments using default formats and appends the formatted output followed by a newline to a byte slice.
Table of Contents
- Introduction
AppendlnFunction Syntax- Examples
- Basic Usage
- Appending Multiple Lines
- Real-World Use Case
- Conclusion
Introduction
The fmt.Appendln function is used to format and append data to a byte slice with a newline character at the end. This function is similar to fmt.Println, but instead of printing to the standard output, it appends the result to a byte slice. This makes it useful for constructing multi-line strings efficiently.
Appendln Function Syntax
The syntax for the fmt.Appendln function is as follows:
func Appendln(b []byte, a ...interface{}) []byte
Parameters:
b: The byte slice to which the formatted data is appended.a: The data to be formatted and appended.
Returns:
- A new byte slice with the formatted data followed by a newline appended.
Examples
Basic Usage
The following example shows how to use the fmt.Appendln function to append a formatted string to a byte slice.
Example
package main
import (
"fmt"
)
func main() {
data := []byte("Hello, ")
data = fmt.Appendln(data, "World")
fmt.Println(string(data))
}
Output:
Hello, World
Appending Multiple Lines
You can use fmt.Appendln to append multiple lines of text to a byte slice.
Example
package main
import (
"fmt"
)
func main() {
data := []byte{}
data = fmt.Appendln(data, "Line 1")
data = fmt.Appendln(data, "Line 2")
data = fmt.Appendln(data, "Line 3")
fmt.Println(string(data))
}
Output:
Line 1
Line 2
Line 3
Real-World Use Case
Constructing a Report
In real-world applications, fmt.Appendln can be used to build reports or logs by appending multiple lines of information to a byte slice.
Example
package main
import (
"fmt"
)
func main() {
report := []byte("Report:\n")
report = fmt.Appendln(report, "Name:", "John Doe")
report = fmt.Appendln(report, "Age:", 30)
report = fmt.Appendln(report, "Occupation:", "Engineer")
fmt.Println(string(report))
}
Output:
Report:
Name: John Doe
Age: 30
Occupation: Engineer
Conclusion
The fmt.Appendln function is used for efficiently appending formatted lines to byte slices in Go. It simplifies the process of building multi-line strings and is ideal for applications that generate reports or logs dynamically.
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