0Pricing
Go Academy · Lesson

The Reader Interface

The heart of Go I/O.

The Reader Interface is a free Go Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The Heart of Go I/O

io.Reader is one interface that unifies all input in Go: files, network connections, strings, compressed streams, and more.

Code that accepts an io.Reader works with any source.

The Interface Definition

The contract is tiny:

  • Read(p []byte) (n int, err error)

It fills p with up to len(p) bytes, returns how many (n) and an error.

Reading from a String

strings.NewReader turns a string into an io.Reader.

package main

import (
	"fmt"
	"strings"
)

func main() {
	r := strings.NewReader("Hello")
	buf := make([]byte, 5)
	n, _ := r.Read(buf)
	fmt.Printf("read %d bytes: %s\n", n, buf[:n])
}

The n-then-err Rule

A Read may return n > 0 bytes and an error in the same call. Always process the n bytes first, then check the error.

Reading in a Loop

Most readers need a loop because a single Read may return fewer bytes than requested.

package main

import (
	"fmt"
	"io"
	"strings"
)

func main() {
	r := strings.NewReader("abcdef")
	buf := make([]byte, 2)
	for {
		n, err := r.Read(buf)
		if n > 0 {
			fmt.Printf("%s ", buf[:n])
		}
		if err == io.EOF {
			break
		}
	}
	fmt.Println()
}

io.ReadAll

When the data is small and bounded, io.ReadAll reads everything into a byte slice for you.

package main

import (
	"fmt"
	"io"
	"strings"
)

func main() {
	r := strings.NewReader("read it all")
	data, _ := io.ReadAll(r)
	fmt.Println(string(data))
}

Readers from Bytes

bytes.NewReader wraps a byte slice, useful when you already have data in memory.

package main

import (
	"bytes"
	"fmt"
	"io"
)

func main() {
	r := bytes.NewReader([]byte{72, 105})
	data, _ := io.ReadAll(r)
	fmt.Println(string(data))
}

Functions That Accept Readers

Write functions taking io.Reader, not concrete types. The same function then reads from a file, a socket, or a test string.

package main

import (
	"fmt"
	"io"
	"strings"
)

func countBytes(r io.Reader) int {
	data, _ := io.ReadAll(r)
	return len(data)
}

func main() {
	fmt.Println(countBytes(strings.NewReader("hello")))
}

io.EOF is Not an Error

Treat io.EOF as a normal signal that the stream ended, not a failure. Only other errors indicate real problems.

LimitReader

io.LimitReader(r, n) wraps a reader to stop after n bytes — handy to cap how much you read.

package main

import (
	"fmt"
	"io"
	"strings"
)

func main() {
	r := io.LimitReader(strings.NewReader("abcdefgh"), 3)
	data, _ := io.ReadAll(r)
	fmt.Println(string(data))
}

Why One Interface Wins

  • Decouples code from data sources
  • Trivial to test with strings/bytes
  • Composable with wrappers like LimitReader, gzip, etc.

Quick Check

Test your io.Reader knowledge.

Recap

You learned the core input interface.

  • Read(p []byte) (int, error) is the whole contract
  • Loop until io.EOF; process n bytes first
  • io.ReadAll, strings/bytes.NewReader, LimitReader
  • Accept io.Reader in your APIs

Frequently asked questions

Is the “The Reader Interface” lesson free?

Yes — the full text of “The Reader Interface” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.

What will I learn in “The Reader Interface”?

The heart of Go I/O. You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Go Academy?

No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “The Reader Interface” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Go Academy lesson?

Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. The Reader Interface
  2. The Writer Interface
  3. io Utility Functions
  4. Implementing Custom Readers
← Back to Go Academy