Streaming IO in Go

Vladimir Vivien
Sep 14, 2017 · 7 min read

The io.Reader

type Reader interface {
Read(p []byte) (n int, err error)
}

Reading Rules (added)

Streaming data from readers

https://github.com/vladimirvivien/learning-go/blob/master/tutorial/io/simple_reader.go
Updated simple_reader.go with fixed error-handler

Implementing a custom io.Reader

https://github.com/vladimirvivien/learning-go/blob/master/tutorial/io/alpha_reader.go
$> go run alpha_reader.go
HelloItsamwhereisthesun

Chaining Readers

https://github.com/vladimirvivien/learning-go/blob/master/tutorial/io/alpha_reader2.go
https://github.com/vladimirvivien/learning-go/blob/master/tutorial/io/alpha_reader3.go

The io.Writer

type Writer interface {
Write(p []byte) (n int, err error)
}

Using writers

https://github.com/vladimirvivien/learning-go/blob/master/tutorial/io/using_writer.go

Implementing a custom io.Writer

https://github.com/vladimirvivien/learning-go/blob/master/tutorial/io/chan_writer.go

Useful types and packages for IO

os.File

https://github.com/vladimirvivien/learning-go/blob/master/tutorial/io/file_write.go
https://github.com/vladimirvivien/learning-go/blob/master/tutorial/io/file_read.go

Standard output, input, and error

https://github.com/vladimirvivien/learning-go/blob/master/tutorial/io/stdout_write.go

io.Copy()

https://github.com/vladimirvivien/learning-go/blob/master/tutorial/io/io_copy.go
https://github.com/vladimirvivien/learning-go/blob/master/tutorial/io/io_copy2.go

io.WriteString()

https://github.com/vladimirvivien/learning-go/blob/master/tutorial/io/write_str.go

Pipe writers and readers

https://github.com/vladimirvivien/learning-go/blob/master/tutorial/io/write_str.go

Buffered IO

https://github.com/vladimirvivien/learning-go/blob/master/tutorial/io/bufread.go

Util package

https://github.com/vladimirvivien/learning-go/blob/master/tutorial/io/io_util.go

Conclusion

Learning the Go Programming Language

Short and insightful posts for newcomers learning the Go programming language

Vladimir Vivien

Written by

Software Eng • Go Programming • Kubernetes • Author http://golang.fyi

Learning the Go Programming Language

Short and insightful posts for newcomers learning the Go programming language