Created
January 30, 2019 00:57
-
-
Save KarthikNayak/fca64990c5724c53c0f23534820c2604 to your computer and use it in GitHub Desktop.
Go Meetup NYC; 29th Jan
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 ( | |
"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