Q:

Side Effects in Go Language

Does anyone know how to write a function with side effects in the Go language? 
I mean like the getchar function in C.
Thanks!

A:

The ReadByte function modifies the state of the buffer.     
package main

import "fmt"

type Buffer struct {
    b []byte
}

func NewBuffer(b []byte) *Buffer {
    return &Buffer{b}
}

func (buf *Buffer) ReadByte() (b byte, eof bool) {
    if len(buf.b) <= 0 {
        return 0, true
    }
    b = buf.b[0]
    buf.b = buf.b[1:]
    return b, false
}

func main() {
    buf := NewBuffer([]byte{1, 2, 3, 4, 5})
    for b, eof := buf.ReadByte(); !eof; b, eof = buf.ReadByte() {
        fmt.Print(b)
    }
    fmt.Println()
}

Output: 12345

