← Back to Go Course | Chapter 13: File IO & OS | Lesson 4 of 7

bufio Package का उपयोग

bufio एक लंबी चिट्ठी को एक बार में एक letter के बजाय पूरे sentences में पढ़ने जैसा है -- यह पढ़ने और लिखने को कहीं ज़्यादा efficient बनाता है।
Syntax
go
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

markup
// 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())
	}
}

Buffered Writing with bufio.Writer

bufio.Writer छोटे writes को एक internal buffer में batch करता है, उन्हें कम, बड़े chunks में underlying writer को भेजते हुए -- जो ज़्यादा efficient है, लेकिन यह गारंटी देने के लिए एक explicit Flush() चाहिए कि असल में सब कुछ लिखा गया।

Note: लिखना खत्म होने पर हमेशा Flush() call करें, नहीं तो आखिरी buffered bytes कभी destination तक न पहुंचें।

उदाहरण: Buffered Writing with bufio.Writer

markup
// 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")
}

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 करता है।

Note: Judge0 में कोई interactive stdin नहीं, इसलिए यह उदाहरण os.Stdin के बजाय एक string reader से input simulate करता है।

उदाहरण: Simulating Reading From Input

markup
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))
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. bufio.NewScanner को scanner के default buffer size से ज़्यादा लंबी किसी single line पर इसे बढ़ाए बिना उपयोग करना, एक चुपचाप truncation या error का कारण बनते हुए।
  2. किसी bufio.Writer पर Flush() call करना भूल जाना, buffered data छोड़ते हुए जो कभी underlying file या connection तक नहीं पहुंचती।
  3. यह मान लेना कि 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.