Standard library / io
io.OffsetWriter.Write()
Writes at the current position plus base offset and advances by the accepted byte count. Expected stdout: '2 .ab.. <nil>\n'.
Formal Syntax & Structure
go syntaxgo
func (o *OffsetWriter) Write(p []byte) (n int, err error)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| p | []byte | Byte slice supplied by the caller. |
Return Value
(n int, err error)
Writes at the current position plus base offset and advances by the accepted byte count.
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) n, e := w.Write([]byte("ab")) fmt.Println(n, string(b.data), e)}