Standard library / io

io.NewOffsetWriter()

Wraps a WriterAt with a base offset and sequential write position; offsets are relative to that base. Expected stdout: '..ab.\n'.

Formal Syntax & Structure

go syntaxgo
func NewOffsetWriter(w WriterAt, off int64) *OffsetWriter

Parameters & Attributes

NameTypeDescription
wWriterAtDestination writer.
offint64Relative or base byte offset, as described above.

Return Value

*OffsetWriter

Wraps a WriterAt with a base offset and sequential write position; offsets are relative to that base.

Working Example

Example Snippetgo
package main
import (  "fmt"  "io")
type memory struct{ data []byte }
func (m *memory) WriteAt(p []byte, off int64) (int, error) {  if off < 0 || off >= int64(len(m.data)) {    return 0, io.ErrShortWrite  }  n := copy(m.data[int(off):], p)  if n < len(p) {    return n, io.ErrShortWrite  }  return n, nil}func main() {  b := &memory{data: []byte(".....")}  w := io.NewOffsetWriter(b, 2)  w.Write([]byte("ab"))  fmt.Println(string(b.data))}
Go Reference