Last active
October 13, 2021 23:53
-
-
Save jrockway/a5d96151e1c69407f491988df708733b to your computer and use it in GitHub Desktop.
Write bytes forever
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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