<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>io on Golang</title><link>https://golang.k5kc.com/categories/io/</link><description>Recent content in io on Golang</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><lastBuildDate>Sun, 16 Aug 2020 00:10:00 +0530</lastBuildDate><atom:link href="https://golang.k5kc.com/categories/io/index.xml" rel="self" type="application/rss+xml"/><item><title>Buffered read</title><link>https://golang.k5kc.com/2020/08/16/buffered-read/</link><pubDate>Sun, 16 Aug 2020 00:10:00 +0530</pubDate><guid>https://golang.k5kc.com/2020/08/16/buffered-read/</guid><description>bufio package provides buffered read functions. Let&amp;rsquo;s see an example:
(1) Create a test.txt file first:
cat test.txt Output:
abcd efg hijk lmn You can see test.txt contains 4 lines.
(2) See the following program:
package main import ( &amp;#34;bufio&amp;#34; &amp;#34;fmt&amp;#34; &amp;#34;io&amp;#34; &amp;#34;log&amp;#34; &amp;#34;os&amp;#34; ) func main() { f, err := os.Open(&amp;#34;test.txt&amp;#34;) if err != nil { log.Fatal(err) } r := bufio.NewReader(f) for { if s, err := r.ReadSlice(&amp;#39;\n&amp;#39;); err == nil || err == io.</description></item><item><title>Decorate types to implement io.Reader interface</title><link>https://golang.k5kc.com/2020/08/16/decorate-types-to-implement-io-reader-interface/</link><pubDate>Sun, 16 Aug 2020 00:10:00 +0530</pubDate><guid>https://golang.k5kc.com/2020/08/16/decorate-types-to-implement-io-reader-interface/</guid><description>The io package has provided a bunch of handy read functions and methods, but unfortunately, they all require the arguments satisfy io.Reader interface. See the following example:
package main import ( &amp;#34;fmt&amp;#34; &amp;#34;io&amp;#34; ) func main() { s := &amp;#34;Hello, world!&amp;#34; p := make([]byte, len(s)) if _, err := io.ReadFull(s, p); err != nil { fmt.Println(err) } else { fmt.Printf(&amp;#34;%s\n&amp;#34;, p) } } Compile above program and an error is generated:</description></item><item><title>Go - Processing JSON object</title><link>https://golang.k5kc.com/2020/08/16/processing-json-object/</link><pubDate>Sun, 16 Aug 2020 00:10:00 +0530</pubDate><guid>https://golang.k5kc.com/2020/08/16/processing-json-object/</guid><description>JSON is a commonly used and powerful data-interchange format, and Go provides a built-in json package to handle it. Let&amp;rsquo; see the following example:
package main import ( &amp;#34;encoding/json&amp;#34; &amp;#34;log&amp;#34; &amp;#34;fmt&amp;#34; ) type People struct { Name string age int Career string `json:&amp;#34;career&amp;#34;` Married bool `json:&amp;#34;,omitempty&amp;#34;` } func main() { p := &amp;amp;People{ Name: &amp;#34;Nan&amp;#34;, age: 34, Career: &amp;#34;Engineer&amp;#34;, } data, err := json.Marshal(p) if err != nil { log.Fatalf(&amp;#34;JSON marshaling failed: %s&amp;#34;, err) } fmt.</description></item><item><title>io.Reader interface</title><link>https://golang.k5kc.com/2020/08/16/io-reader-interfaces/</link><pubDate>Sun, 16 Aug 2020 00:10:00 +0530</pubDate><guid>https://golang.k5kc.com/2020/08/16/io-reader-interfaces/</guid><description>io.Reader interface is a basic and very frequently-used interface:
type Reader interface { Read(p []byte) (n int, err error) } For every type who satisfies the io.Reader interface, you can imagine it&amp;rsquo;s a pipe. Someone writes contents into one end of the pipe, and you can use Read() method which the type has provided to read content from the other end of the pipe. No matter it is a common file, a network socket, and so on.</description></item><item><title>io.Writer interface</title><link>https://golang.k5kc.com/2020/08/16/io-writer-interface/</link><pubDate>Sun, 16 Aug 2020 00:10:00 +0530</pubDate><guid>https://golang.k5kc.com/2020/08/16/io-writer-interface/</guid><description>The inverse of io.Reader is io.Writer interface:
type Writer interface { Write(p []byte) (n int, err error) } Compared to io.Reader, since you no need to consider io.EOF error, the process of Write method is simple:
(1) err == nil: All the data in p is written successfully;
(2) err != nil: The data in p is partially or not written at all.
Let&amp;rsquo;s see an example:
package main import ( &amp;#34;log&amp;#34; &amp;#34;os&amp;#34; ) func main() { f, err := os.</description></item></channel></rss>