Welcome to this comprehensive guide on Go’s I/O library. This tutorial will help you understand the fundamental concepts of input (Reader) and output (Writer) interfaces along with practical examples for file handling, HTTP responses, database operations, and more. The Go standard library abstracts these tasks into common interfaces, making your code more modular and flexible.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.


The Reader Interface
The Reader interface in Go is designed to provide input functionality from various data sources. It is defined with a single method,Read, which accepts a byte slice and returns the number of bytes read along with any error encountered.
The Writer Interface
The Writer interface offers a generic way to output data. It provides a single method,Write, which accepts a byte slice and returns the number of bytes written along with any error that may occur.
Go uses implicit interface implementation. Unlike Java or other languages, there is no need to explicitly declare that a type implements an interface. If a type implements all methods of an interface, it is automatically considered to satisfy that interface.
A Custom Interface Example: Shape
To illustrate the power and flexibility of interfaces in Go, consider the following example where we create a custom interface namedShape. This interface includes two methods, Area and Perimeter, both of which return a float64.
We will implement the Shape interface using a struct called Rectangle, which contains two fields: Length and Width.
Revisiting the Reader Interface
Let’s delve deeper into the Reader interface using an example from thestrings package. Consider the following source code snippet for a reader.go file that defines a Reader struct implementing the io.Reader interface.
Reader Struct Definition
Method for Unread Length
A method to get the number of bytes still unread is provided:Implementing the Read Method
TheRead method is the core implementation that satisfies the io.Reader interface:
Additional Helper Functions
There are helper functions to facilitate working with this Reader:Using the NewReader Function
TheNewReader function allows you to create a new reader instance from a string quickly. The following example shows how to read a string in chunks of 4 bytes:
Reading in a Loop
To read the entire string, you can use a loop that continues until an error (such asio.EOF) is encountered. See the following example:
EOF error.
The Writer Interface and Using io.Copy
The Writer interface simplifies the process of outputting data to various destinations. One common use case is copying data from a Reader to a Writer using theio.Copy function. In this example, we create a Reader and use io.Copy to write its contents to os.Stdout:
io.Copy function efficiently transfers data from a source (which implements io.Reader) to a destination (which implements io.Writer). In this example, os.Stdout is used as the destination writer.
The Writer Implementation in os.File
Theos.Stdout variable is of type *os.File. The File struct in the os package implements the Write method to satisfy the Writer interface. Below is an excerpt from the source code of the Write method:
File type implements the Write method, it inherently satisfies the Writer interface, allowing its seamless use as the destination in functions like io.Copy.
Conclusion
In this tutorial, we explored how Go’s I/O library encapsulates input and output operations through the Reader and Writer interfaces. We looked at:- Basic definitions and implementations of the Reader and Writer interfaces.
- A custom interface example using the
Shapeinterface for aRectangle. - Detailed examples from the
stringspackage that implement and demonstrate the usage of the Reader interface. - The use of the
io.Copyfunction to connect Reader and Writer interfaces, along with insights into the underlying implementation inos.File.