Standard library / io
io.WriterAt
Writes at explicit offsets; short writes require an error. Nonoverlapping writes may be made concurrently under its contract. Expected stdout: '2 <nil>\n'.
Formal Syntax & Structure
go syntaxgo
type WriterAt interface { WriteAt(p []byte, off int64) (n int, err error)}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() { var w io.WriterAt = &memory{data: make([]byte, 3)} n, e := w.WriteAt([]byte("ab"), 1) fmt.Println(n, e)}