io.Reader और io.Writer
In this page:
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
// 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")
}
Login to try C/C++/Java/PHP code in the editor
The io.Reader Interface
io.Reader को Read(p []byte) (int, error) चाहिए, जो दी गई buffer को जितना data उपलब्ध हो उससे भरता है और बताता है कि कितने bytes लिखे, साथ ही एक error (stream खत्म होने पर io.EOF)।
उदाहरण: The io.Reader Interface
// 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]))
}
Login to try C/C++/Java/PHP code in the editor
Reading Until EOF
चूंकि एक single Read call पूरा stream return नहीं कर सकता, जो code सब कुछ चाहता है वह आमतौर पर loop करता है, Read को बार-बार call करता है जब तक इसे io.EOF error न मिले, या एक helper जैसे io.ReadAll उपयोग करता है जो यह आपके लिए करता है।
उदाहरण: Reading Until EOF
// 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))
}
Login to try C/C++/Java/PHP code in the editor
- यह मान लेना कि Read हमेशा एक call में पूरी buffer भर देता है -- यह मांगे से कम bytes return कर सकता है, और callers को n जांचना चाहिए और ज़रूरत पड़ने पर loop करना चाहिए।
- Read से io.EOF error को नज़रअंदाज़ करना, जो एक असली error condition के बजाय stream के अंत का संकेत देता है।
- यह भूल जाना कि 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: