Skip to content

Instantly share code, notes, and snippets.

@jrockway
Last active October 13, 2021 23:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrockway/a5d96151e1c69407f491988df708733b to your computer and use it in GitHub Desktop.
Save jrockway/a5d96151e1c69407f491988df708733b to your computer and use it in GitHub Desktop.
Write bytes forever
package main
import (
"fmt"
"os"
"sync"
"time"
)
func main() {
var mu sync.Mutex
var wrote int
var lastWrite time.Time
go func() {
for range time.Tick(time.Second) {
mu.Lock()
fmt.Fprintf(os.Stderr, "wrote %d bytes %v ago\n", wrote, time.Since(lastWrite))
mu.Unlock()
}
}()
for {
n, err := os.Stdout.Write([]byte("A"))
if err != nil {
break
}
mu.Lock()
wrote += n
lastWrite = time.Now()
mu.Unlock()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment