0Pricing
Go Academy · Lesson

The Writer Interface

Write to anything.

The Writer Interface is a free Go Academy lesson on CoddyKit — lesson 2 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.

Write to Anything

io.Writer unifies all output: files, network, buffers, stdout, HTTP responses. One interface, many destinations.

The Interface Definition

The contract:

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

It writes p and returns how many bytes were written plus any error.

Writing to a Buffer

bytes.Buffer implements io.Writer and collects output in memory.

package main

import (
	"bytes"
	"fmt"
)

func main() {
	var buf bytes.Buffer
	buf.Write([]byte("Hello, "))
	buf.WriteString("Writer!")
	fmt.Println(buf.String())
}

Writing to Stdout

os.Stdout is an io.Writer. So is os.Stderr.

package main

import (
	"io"
	"os"
)

func main() {
	io.WriteString(os.Stdout, "straight to stdout\n")
}

fmt.Fprintf and Writers

The Fprint family writes formatted text to any io.Writer.

package main

import (
	"bytes"
	"fmt"
)

func main() {
	var buf bytes.Buffer
	fmt.Fprintf(&buf, "x = %d\n", 42)
	fmt.Print(buf.String())
}

The Short Write Error

If Write returns n < len(p) with a nil error, that is a contract violation called a short write. Compliant writers report it as io.ErrShortWrite.

Functions That Accept Writers

Write functions taking io.Writer so the same logic targets a file, a buffer, or the network.

package main

import (
	"fmt"
	"io"
	"os"
)

func greet(w io.Writer, name string) {
	fmt.Fprintf(w, "Hi, %s\n", name)
}

func main() {
	greet(os.Stdout, "Ada")
}

Decoupling Output for Tests

Because functions take io.Writer, tests capture output into a bytes.Buffer and assert on it — no need to touch real files.

package main

import (
	"bytes"
	"fmt"
)

func emit(w *bytes.Buffer) { fmt.Fprint(w, "result") }

func main() {
	var b bytes.Buffer
	emit(&b)
	fmt.Println("captured:", b.String())
}

Writing Bytes vs Strings

Write takes a byte slice; io.WriteString is a convenience for strings that avoids an allocation when the writer supports it.

package main

import (
	"io"
	"os"
)

func main() {
	os.Stdout.Write([]byte("bytes\n"))
	io.WriteString(os.Stdout, "string\n")
}

Always Check Write Errors

A write to a file or socket can fail mid-stream (disk full, connection dropped). Check the returned error, especially for non-memory writers.

One Interface, Many Targets

  • Files, buffers, stdout/stderr
  • HTTP response writers
  • Compression and encryption wrappers

All satisfy io.Writer, so your code stays destination-agnostic.

Quick Check

Test your io.Writer knowledge.

Recap

You learned the core output interface.

  • Write(p []byte) (int, error) is the whole contract
  • bytes.Buffer, os.Stdout, HTTP responses all implement it
  • Works with fmt.Fprintf and io.WriteString
  • Accept io.Writer for flexible, testable APIs

Frequently asked questions

Is the “The Writer Interface” lesson free?

Yes — the full text of “The Writer 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 Writer Interface”?

Write to anything. 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 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “The Writer 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