Output Functions
Output Functions
Go provides several built-in functions to handle output operations, primarily through the fmt package. These functions enable printing text, variables, formatted strings, and more to the console or other output streams.
1. fmt.Print()
The fmt.Print() function writes the provided arguments to the standard output without adding a newline at the end. It concatenates the values with a space if they are not explicitly formatted.
Syntax:
fmt.Print(value1, value2, ...)
Example:
package main import "fmt" func main() { fmt.Print("Hello, ") fmt.Print("World!") }
Output:
Hello, World!
2. fmt.Println()
The fmt.Println() function is similar to fmt.Print() but automatically appends a newline (\n) at the end. Additionally, it inserts spaces between multiple arguments.
Syntax:
fmt.Println(value1, value2, ...)
Example:
package main import "fmt" func main() { fmt.Println("Hello,") fmt.Println("World!") }
Output:
Hello, World!
3. fmt.Printf()
The fmt.Printf() function allows formatted output using format specifiers. This method provides more control over the appearance of the output, such as specifying types, widths, and precision for different values.
Syntax:
fmt.Printf(format_string, value1, value2, ...)
Example:
package main import "fmt" func main() { name := "Alice" age := 25 fmt.Printf("Name: %s, Age: %d\n", name, age) }
Output:
Name: Alice, Age: 25
Common Format Specifiers:
Specifier | Description |
---|---|
%d | Integer |
%f | Floating-point number |
%s | String |
%t | Boolean |
%v | Any value (default format) |
%T | Type of the value |
4. fmt.Fprint()
The fmt.Fprint() function writes output to a specified writer (e.g., a file, buffer, or network connection) instead of standard output.
Syntax:
fmt.Fprint(writer, value1, value2, ...)
Example:
package main import ( "fmt" "os" ) func main() { file, _ := os.Create("output.txt") defer file.Close() fmt.Fprint(file, "Go Lang :Writing Content or text to a file using Fprint.") }
This will save the text "Go Lang :Writing Content or text to a file using Fprint." in output.txt rather than displaying it on the console.
5. fmt.Fprintf()
The fmt.Fprintf() function works like fmt.Printf(), but instead of printing to the console, it sends the formatted output to a specified writer.
Syntax:
fmt.Fprintf(writer, format_string, value1, value2, ...)
Example:
package main import ( "fmt" "os" ) func main() { file, _ := os.Create("output.txt") defer file.Close() name := "Alice" fmt.Fprintf(file, "Hello, %s!\n", name) }
This will write "Hello, Alice!" to output.txt.
6. fmt.Fprintln()
The fmt.Fprintln() function is similar to fmt.Fprint(), but it appends a newline at the end of the output.
Syntax:
fmt.Fprintln(writer, value1, value2, ...)
Example:
package main import ( "fmt" "os" ) func main() { file, _ := os.Create("output.txt") defer file.Close() fmt.Fprintln(file, "This text will be recorded in the file.") }
This writes "This text will be recorded in the file." to output.txt, followed by a newline.
Conclusion
Go provides a range of output functions through the fmt package, enabling developers to print simple messages, formatted text, and even direct output to files or other writers. Understanding these functions helps in effectively displaying information in Go programs.
PreviousNext