io Utility Functions
Copy, TeeReader, MultiWriter.
io Utility Functions is a free Go Academy lesson on CoddyKit — lesson 3 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 io Toolbox
The io package ships small, powerful helpers that combine readers and writers: Copy, TeeReader, MultiWriter, and more.
io.Copy
io.Copy(dst, src) streams all bytes from a reader to a writer and returns the count.
package main
import (
"fmt"
"io"
"os"
"strings"
)
func main() {
n, _ := io.Copy(os.Stdout, strings.NewReader("copied\n"))
fmt.Println("bytes:", n)
}io.CopyN
io.CopyN(dst, src, n) copies exactly n bytes — useful for reading a known-length prefix.
package main
import (
"io"
"os"
"strings"
)
func main() {
io.CopyN(os.Stdout, strings.NewReader("abcdef"), 3)
os.Stdout.WriteString("\n")
}io.MultiWriter
io.MultiWriter fans one write out to several writers at once — like the Unix tee command.
package main
import (
"bytes"
"fmt"
"io"
"os"
)
func main() {
var buf bytes.Buffer
w := io.MultiWriter(os.Stdout, &buf)
io.WriteString(w, "to both\n")
fmt.Println("buffer copy:", buf.String())
}io.TeeReader
io.TeeReader(r, w) returns a reader that copies everything it reads into w — perfect for logging or hashing data as it flows.
package main
import (
"bytes"
"fmt"
"io"
"strings"
)
func main() {
var spy bytes.Buffer
tee := io.TeeReader(strings.NewReader("stream"), &spy)
io.ReadAll(tee)
fmt.Println("observed:", spy.String())
}io.MultiReader
io.MultiReader concatenates several readers into one continuous stream.
package main
import (
"fmt"
"io"
"strings"
)
func main() {
r := io.MultiReader(strings.NewReader("Hello, "), strings.NewReader("World"))
data, _ := io.ReadAll(r)
fmt.Println(string(data))
}io.Discard
io.Discard is a writer that throws everything away — useful to drain a reader you do not need.
package main
import (
"fmt"
"io"
"strings"
)
func main() {
n, _ := io.Copy(io.Discard, strings.NewReader("ignored"))
fmt.Println("discarded:", n, "bytes")
}Hashing with TeeReader
A classic combo: read a stream for its real purpose while a TeeReader feeds the same bytes into a hash, so you compute a checksum in one pass.
io.Pipe
io.Pipe connects a writer to a reader in memory, often across goroutines, for streaming producer/consumer patterns.
Choosing the Right Helper
- Move data:
Copy/CopyN - Duplicate output:
MultiWriter - Observe input:
TeeReader - Join inputs:
MultiReader - Drain input:
Discard
Composability
These helpers all speak io.Reader/io.Writer, so they nest and chain freely to build sophisticated pipelines from tiny parts.
Quick Check
Test your io utilities knowledge.
Recap
You learned the io utility helpers.
Copy/CopyNstream dataMultiWriterduplicates outputTeeReaderobserves inputMultiReaderjoins,Discarddrains- All compose via the Reader/Writer interfaces
Frequently asked questions
Is the “io Utility Functions” lesson free?
Yes — the full text of “io Utility Functions” 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 “io Utility Functions”?
Copy, TeeReader, MultiWriter. 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 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “io Utility Functions” 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
- The Reader Interface
- The Writer Interface
- io Utility Functions
- Implementing Custom Readers