Skip to content

Instantly share code, notes, and snippets.

@KarthikNayak
Created January 30, 2019 00:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save KarthikNayak/fca64990c5724c53c0f23534820c2604 to your computer and use it in GitHub Desktop.
Save KarthikNayak/fca64990c5724c53c0f23534820c2604 to your computer and use it in GitHub Desktop.
Go Meetup NYC; 29th Jan
package main
import (
"io"
"os"
"fmt"
"time"
)
type SlowWriter struct {
w io.Writer
bps int
}
func (s *SlowWriter) Write(p []byte) (n int, err error) {
bytesPerSecond := s.bps/8
finalN := 0
fmt.Println(bytesPerSecond)
for i := 0; i < len(p); i+= bytesPerSecond {
n, err := s.w.Write(p[i:i + bytesPerSecond])
finalN = finalN + n
if err != nil {
return finalN, err
}
time.Sleep(time.Second)
}
return finalN, nil
}
func main() {
data := []byte("random data")
s := SlowWriter{w: os.Stdout, bps: 8}
s.Write(data)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment