← Back to Go Course | Chapter 7: Interfaces | Lesson 6 of 7

io.Reader और io.Writer

io.Reader और io.Writer data के लिए universal plug shapes जैसे हैं -- कोई भी चीज़ जो bytes दे सके एक में fit होती है, और कोई भी चीज़ जो bytes accept कर सके दूसरे में, दूसरे छोर पर चाहे जो भी हो।
Syntax
go
type Reader interface {
    Read(p []byte) (n int, err error)
}

type Writer interface {
    Write(p []byte) (n int, err error)
}

The io.Writer Interface

io.Writer को एक single method चाहिए, Write(p []byte) (int, error), जो bytes की एक slice accept करता है और बताता है कि कितने लिखे गए। चूंकि यह इतना minimal है, files से लेकर network sockets से लेकर fmt.Fprintf की destination तक सब इसे implement कर सकते हैं।

उदाहरण: The io.Writer Interface

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the packages this program uses
import (
	"fmt"
	"os"
)

// main is where the program starts running
func main() {
	fmt.Fprintln(os.Stdout, "written through an io.Writer")
}

The io.Reader Interface

io.Reader को Read(p []byte) (int, error) चाहिए, जो दी गई buffer को जितना data उपलब्ध हो उससे भरता है और बताता है कि कितने bytes लिखे, साथ ही एक error (stream खत्म होने पर io.EOF)।

Note: Read buffer की length से कम bytes return कर सकता है -- हमेशा सिर्फ err नहीं, n भी जांचें।

उदाहरण: The io.Reader Interface

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the packages this program uses
import (
	"fmt"
	"strings"
)

// main is where the program starts running
func main() {
	// Short declaration: create r and infer the type
	r := strings.NewReader("Go rocks")
	// Short declaration: create buf and infer the type
	buf := make([]byte, 4)
	n, _ := r.Read(buf)
	// Print the values followed by a newline
	fmt.Println(n, string(buf[:n]))
}

Reading Until EOF

चूंकि एक single Read call पूरा stream return नहीं कर सकता, जो code सब कुछ चाहता है वह आमतौर पर loop करता है, Read को बार-बार call करता है जब तक इसे io.EOF error न मिले, या एक helper जैसे io.ReadAll उपयोग करता है जो यह आपके लिए करता है।

उदाहरण: Reading Until EOF

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the packages this program uses
import (
	"fmt"
	"io/ioutil"
	"strings"
)

// main is where the program starts running
func main() {
	// Short declaration: create r and infer the type
	r := strings.NewReader("Hello, io.Reader!")
	// Short declaration: create data, err and infer the type
	data, err := ioutil.ReadAll(r)
	// Handle the error if one occurred
	if err != nil {
		// Print the values followed by a newline
		fmt.Println("error:", err)
		return
	}
	// Print the values followed by a newline
	fmt.Println(string(data))
}
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. यह मान लेना कि Read हमेशा एक call में पूरी buffer भर देता है -- यह मांगे से कम bytes return कर सकता है, और callers को n जांचना चाहिए और ज़रूरत पड़ने पर loop करना चाहिए।
  2. Read से io.EOF error को नज़रअंदाज़ करना, जो एक असली error condition के बजाय stream के अंत का संकेत देता है।
  3. यह भूल जाना कि Write को असल में लिखे bytes की संख्या return करनी चाहिए, और callers को verify करना चाहिए कि यह len(p) से मेल खाती है।
चैप्टर सारांश
  • io.Reader Read(p []byte) (n int, err error) define करता है; io.Writer Write(p []byte) (n int, err error) define करता है।
  • ये छोटे, one-method interfaces files, network connections, और in-memory buffers सभी को interchangeably उपयोग होने देते हैं।
  • io.EOF वह special error है जो signal देता है कि किसी Reader के पास अब और data नहीं।
  • strings.NewReader और bytes.Buffer असली I/O के बिना एक io.Reader/io.Writer पाने के आम तरीके हैं।
🔒

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.