- Get link
- X
- Other Apps
In Go, input and output operations are handled through the
fmt package for standard I/O and the os package for file-based and advanced I/O operations. The language offers simple and efficient tools for handling user input, displaying output, and interacting with files.Output in Go
The fmt package provides functions for formatted output.
Basic Output Examples:
- Print a Line:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!") // Prints with a newline
}
- Printing Without a Newline:
package main
import "fmt"
func main() {
fmt.Print("Hello, ") // No newline
fmt.Print("World!") // Continues on the same line
}
- Formatted Output:
package main
import "fmt"
func main() {
name := "Alice"
age := 30
fmt.Printf("Name: %s, Age: %d\n", name, age) // Uses format specifiers
}
| Format Specifier | Description |
|---|---|
%s | String |
%d | Decimal integer |
%f | Floating-point number |
%v | Default format for a value |
%T | Type of a value |
Input in Go
Input can be read using functions like fmt.Scan, fmt.Scanln, or bufio for buffered input.
Reading Input with fmt.Scan:
package main
import "fmt"
func main() {
var name string
var age int
fmt.Print("Enter your name: ")
fmt.Scan(&name) // Reads input into the variable
fmt.Print("Enter your age: ")
fmt.Scan(&age)
fmt.Printf("Hello, %s! You are %d years old.\n", name, age)
}
&nameand&agepass the memory addresses of the variables to store user input.- Values must be entered sequentially.
Reading Input with fmt.Scanln:
Scanln is similar to Scan but reads until a newline.
package main
import "fmt"
func main() {
var input string
fmt.Print("Enter some text: ")
fmt.Scanln(&input)
fmt.Println("You entered:", input)
}
Reading Input with bufio:
For more complex input scenarios, use the bufio package.
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
reader := bufio.NewReader(os.Stdin) // Create a buffered reader
fmt.Print("Enter your text: ")
input, _ := reader.ReadString('\n') // Reads until newline
fmt.Println("You entered:", input)
}
ReadString('\n')reads the input until a newline character.- This approach is preferred for multiline or large inputs.
File I/O in Go
File operations are handled using the os and io packages.
Writing to a File:
package main
import (
"os"
)
func main() {
file, err := os.Create("example.txt") // Create or overwrite a file
if err != nil {
panic(err)
}
defer file.Close() // Ensure the file is closed after use
file.WriteString("Hello, file!") // Write to the file
}
Reading from a File:
package main
import (
"fmt"
"os"
)
func main() {
file, err := os.Open("example.txt") // Open the file
if err != nil {
panic(err)
}
defer file.Close()
data := make([]byte, 100) // Buffer to hold file data
count, err := file.Read(data) // Read file content
if err != nil {
panic(err)
}
fmt.Printf("Read %d bytes: %s\n", count, data)
}
Summary Table of Common Functions
| Operation | Function | Description |
|---|---|---|
| Print Output | fmt.Print, fmt.Println | Displays text with or without a newline. |
| Formatted Output | fmt.Printf | Displays text with formatting. |
| Read Input (Simple) | fmt.Scan, fmt.Scanln | Reads input directly into variables. |
| Read Input (Buffered) | bufio.NewReader | Reads larger or complex input efficiently. |
| Write to a File | os.Create, file.Write | Creates and writes data to a file. |
| Read from a File | os.Open, file.Read | Reads data from a file into a buffer. |
Why Use Go for I/O?
- Simplicity: Intuitive and easy-to-read APIs for I/O operations.
- Efficiency: Optimized for performance with low-level control.
- Flexibility: Supports standard, buffered, and file I/O seamlessly.
Go's approach to I/O aligns with its philosophy of simplicity and performance, making it an excellent choice for building robust and efficient applications.
- Get link
- X
- Other Apps

👾👾👾👾👾
ReplyDeleteهعمل كومنت عشان بلبل قال فيوز بس
ReplyDelete