🎓 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.Print function in Golang is part of the fmt package and is used to output data to the standard output (usually the console). It formats the data using the default formats for its operands and writes them in a single line without adding any extra spaces or newlines between the operands.
Table of Contents
- Introduction
PrintFunction Syntax- Examples
- Basic Usage
- Printing Variables
- Real-World Use Case
- Conclusion
Introduction
The fmt.Print function is a simple and efficient way to display data in a Go program. It can be used to output strings, numbers, variables, or any combination of data types. Unlike fmt.Println, fmt.Print does not add a newline at the end of the output, which allows you to format your output precisely as needed.
Print Function Syntax
The syntax for the fmt.Print function is as follows:
func Print(a ...interface{}) (n int, err error)
Parameters:
a: The data to be printed. This can be a mix of strings, numbers, and other data types.
Returns:
n: The number of bytes written.err: An error if one occurred during writing.
Examples
Basic Usage
This example demonstrates how to use the fmt.Print function to output text and numbers.
Example
package main
import (
"fmt"
)
func main() {
// Use fmt.Print to output a string and a number
fmt.Print("The answer is ", 42)
}
Output:
The answer is 42
Printing Variables
You can use fmt.Print to print the values of variables directly.
Example
package main
import (
"fmt"
)
func main() {
name := "Alice"
age := 30
// Use fmt.Print to print variables
fmt.Print("Name: ", name, ", Age: ", age)
}
Output:
Name: Alice, Age: 30
Real-World Use Case
Building Custom Outputs
In real-world applications, fmt.Print can be used to build custom output messages without automatic newline characters. This is useful for creating more controlled console outputs.
Example
package main
import (
"fmt"
)
func main() {
product := "Laptop"
price := 999.99
// Use fmt.Print to build a custom output message
fmt.Print("Product: ", product)
fmt.Print(", Price: quot;, price)
}
Output:
Product: Laptop, Price: $999.99
Conclusion
The fmt.Print function is a straightforward way to print data to the console without additional formatting. It is ideal for applications that require precise control over output formatting. By using fmt.Print, you can efficiently display data and messages in your Go programs.
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