Standard library / io

io.OffsetWriter.WriteAt()

Writes at a supplied relative offset without changing the sequential position. Expected stdout: '.y.x.\n'.

Formal Syntax & Structure

go syntaxgo
func (o *OffsetWriter) WriteAt(p []byte, off int64) (n int, err error)

Parameters & Attributes

NameTypeDescription
p[]byteByte slice supplied by the caller.
offint64Relative or base byte offset, as described above.

Return Value

(n int, err error)

Writes at a supplied relative offset without changing the sequential position.

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, 1)  w.WriteAt([]byte("x"), 2)  w.Write([]byte("y"))  fmt.Println(string(b.data))}
Go Reference