Skip to content
Go: Account for partial delivery with io.MultiWriter

Go: Account for partial delivery with io.MultiWriter

November 19, 2026

io.MultiWriter sends each write to its destinations in order and stops at the first error. Earlier destinations may already contain the data. Retrying the same write can duplicate their output while later destinations still receive nothing.

Set up

Tested on Linux amd64 with Go 1.26.10. Use Bash with Go on your PATH, a text editor, and the mktemp, rm, and rmdir utilities. The example uses only the standard library. Keep the same terminal through cleanup, and stop if setup fails before saving the source.

start_dir="$PWD"
work_dir="$(mktemp -d)" || exit 1
cd -- "$work_dir" || exit 1

Run the example

Save as go-multiwriter-partial-delivery.go:

package main

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

var destinationFailed = errors.New("destination unavailable")

type failingWriter struct{}

func (failingWriter) Write(p []byte) (int, error) {
	return 0, destinationFailed
}

func main() {
	var first, last bytes.Buffer
	writer := io.MultiWriter(&first, failingWriter{}, &last)
	n, err := writer.Write([]byte("event\n"))
	fmt.Printf("reported bytes=%d failed=%t\n", n, errors.Is(err, destinationFailed))
	fmt.Printf("first=%q last=%q\n", first.String(), last.String())
	n, err = writer.Write([]byte("event\n"))
	fmt.Printf("retry bytes=%d failed=%t\n", n, errors.Is(err, destinationFailed))
	fmt.Printf("after retry: first=%q last=%q\n", first.String(), last.String())
}

Run:

go run go-multiwriter-partial-delivery.go

Observed output:

reported bytes=0 failed=true
first="event\n" last=""
retry bytes=0 failed=true
after retry: first="event\nevent\n" last=""

Read the result

The first buffer receives the complete event before the deliberately failing writer returns an error. The final buffer is never reached. The reported byte count is zero in this case, but that does not mean no destination changed.

The second call deliberately repeats the failed operation to expose the retry behavior. It writes another copy into the first buffer and fails at the same destination again. The example handles the expected error with a boolean check and performs no external I/O.

Apply it carefully

Use this helper when sequential duplication and stopping on the first failure match your delivery policy. If every destination must be attempted, call each one and record its result. For retries, track each destination’s progress and decide how duplicate records are handled.

There is no rollback of an earlier write. A successful write also does not establish that a file is durable or that a buffered writer has flushed. Manage flushing and closing through the original destinations; the combined value exposes the writer interface. The example’s failing destination writes no bytes, but real failures can happen after a partial write.

See MultiWriter and the Writer contract.

Clean up

Remove the saved source and the empty scratch directory. Go manages its build cache separately. If the directory is not empty, inspect its contents before removing anything else.

rm -- go-multiwriter-partial-delivery.go
cd -- "$start_dir" || exit 1
rmdir -- "$work_dir"
unset start_dir work_dir

Acknowledgements

This post was created with assistance from AI.

Last updated on