bufio Package का उपयोग
In this page:
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
line := scanner.Text()
}
w := bufio.NewWriter(writer)
defer w.Flush()
Reading Lines with bufio.Scanner
bufio.NewScanner किसी भी io.Reader को wrap करता है और, bufio.ScanLines (default) के साथ combined, आपको Scan() को बार-बार call करके और Text() पढ़कर एक समय में एक line input पढ़ने देता है।
उदाहरण: Reading Lines with bufio.Scanner
// Every Go file belongs to a package; main builds an executable
package main
// Import the packages this program uses
import (
"bufio"
"fmt"
"strings"
)
// main is where the program starts running
func main() {
// Short declaration: create text and infer the type
text := "line one\nline two\nline three"
// Short declaration: create scanner and infer the type
scanner := bufio.NewScanner(strings.NewReader(text))
// Repeat while scanner.Scan() is true
for scanner.Scan() {
// Print the values followed by a newline
fmt.Println("read:", scanner.Text())
}
}
Login to try C/C++/Java/PHP code in the editor
Buffered Writing with bufio.Writer
bufio.Writer छोटे writes को एक internal buffer में batch करता है, उन्हें कम, बड़े chunks में underlying writer को भेजते हुए -- जो ज़्यादा efficient है, लेकिन यह गारंटी देने के लिए एक explicit Flush() चाहिए कि असल में सब कुछ लिखा गया।
उदाहरण: Buffered Writing with bufio.Writer
// Every Go file belongs to a package; main builds an executable
package main
// Import the packages this program uses
import (
"bufio"
"fmt"
"os"
)
// main is where the program starts running
func main() {
// Short declaration: create writer and infer the type
writer := bufio.NewWriter(os.Stdout)
writer.WriteString("buffered ")
writer.WriteString("output\n")
writer.Flush()
// Print the values followed by a newline
fmt.Println("flush complete")
}
Login to try C/C++/Java/PHP code in the editor
Simulating Reading From Input
किसी असली interactive program में, bufio.NewReader(os.Stdin) user input को line by line पढ़ता है, लेकिन चूंकि इस sandbox में कोई live terminal input नहीं, यहां वही bufio.Reader API एक in-memory string से पढ़ने का demonstrate करता है।
उदाहरण: Simulating Reading From Input
package main
import (
"bufio"
"fmt"
"strings"
)
func main() {
// Simulated stdin content (real os.Stdin would be used interactively)
reader := bufio.NewReader(strings.NewReader("Gopher\n"))
name, _ := reader.ReadString('\n')
fmt.Println("hello,", strings.TrimSpace(name))
}
Login to try C/C++/Java/PHP code in the editor
- bufio.NewScanner को scanner के default buffer size से ज़्यादा लंबी किसी single line पर इसे बढ़ाए बिना उपयोग करना, एक चुपचाप truncation या error का कारण बनते हुए।
- किसी bufio.Writer पर Flush() call करना भूल जाना, buffered data छोड़ते हुए जो कभी underlying file या connection तक नहीं पहुंचती।
- यह मान लेना कि Scan() को फिर call करने के बाद Scanner.Text() अब भी आखिरी line का data रखता है -- यह हर call पर overwrite हो जाता है।
- bufio.Scanner input को efficiently line by line (या दूसरे split functions से) पढ़ता है।
- bufio.Reader/Writer किसी io.Reader/Writer को एक internal buffer के साथ wrap करते हैं underlying I/O operations की संख्या घटाने के लिए।
- किसी bufio.Writer के buffered data को यह गारंटी देने के लिए explicitly Flush() से flush किया जाना चाहिए कि यह असल में लिखा गया।
- bufio आमतौर पर files पढ़ने या (interactive programs में) os.Stdin से पढ़ने के लिए उपयोग होता है।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: