[lectures] Update io slides.

This commit is contained in:
Arseny Balobanov 2022-04-21 18:13:36 +03:00
parent f4feebffbe
commit edd97e0f1b
5 changed files with 17 additions and 16 deletions

View file

@ -2,10 +2,9 @@ package main
import (
"io"
"io/ioutil"
"strings"
)
func main() {
_, _ = io.Copy(ioutil.Discard, strings.NewReader("nothing of use"))
_, _ = io.Copy(io.Discard, strings.NewReader("nothing of use"))
}

View file

@ -122,7 +122,7 @@ Problem: proxy a chunked HTTP in a stream.
.play -edit httpchunking/solution3/main.go /^func transfer/,/^}/
* ioutil
* ioutil (deprecated)
Package *io/ioutil* implements some I/O utility functions.
@ -216,7 +216,7 @@ Can we avoid using an intermediate buffer entirely?
* Example: sendfile
- http.ResponseWriter's is an io.ReaderFrom that uses the implementation of underlying tcp conn.
- http.ResponseWriter is an io.ReaderFrom that uses the implementation of underlying tcp conn.
// ReadFrom is here to optimize copying from an *os.File regular file
// to a *net.TCPConn with sendfile.
@ -226,11 +226,15 @@ Can we avoid using an intermediate buffer entirely?
func (c *TCPConn) ReadFrom(r io.Reader) (int64, error) {
* ioutil.Discard
* io.Discard
// Discard is an io.Writer on which all Write calls succeed
// without doing anything.
var Discard io.Writer = devNull(0)
var Discard Writer = discard{}
type discard struct{}
func (discard) Write(p []byte) (int, error) {
return len(p), nil
}
- Implements io.ReaderFrom!
@ -409,7 +413,7 @@ Useful when you want to use code that takes an io.Writer, and store the results
return string(b.buf[b.off:])
}
* strings.Builder vs strings.Builder
* bytes.Buffer vs strings.Builder
.play -edit stringsbuilder/main.go
@ -432,7 +436,7 @@ Useful when you want to use code that takes an io.Writer, and store the results
- useful if you don't want to read the whole file into memory
- has no internal buffers
*os.ReadFile* (ioutil.ReadFile) reads an entire file into memory (as a []byte) in a single call
*os.ReadFile* reads an entire file into memory (as a []byte) in a single call
- allocates a byte slice of the correct size (no need to Read + append in a loop)
- closes the file

View file

@ -2,7 +2,7 @@ package main
import (
"fmt"
"io/ioutil"
"io"
"log"
"strings"
)
@ -11,7 +11,7 @@ func main() {
r := strings.NewReader("Go is a general-purpose language " +
"designed with systems programming in mind.")
b, err := ioutil.ReadAll(r)
b, err := io.ReadAll(r)
if err != nil {
log.Fatal(err)
}

View file

@ -2,7 +2,6 @@ package main
import (
"io"
"io/ioutil"
"log"
"net/http"
"os"
@ -18,7 +17,7 @@ func main() {
func readFileHandler(w http.ResponseWriter, r *http.Request) {
filename := r.URL.Query().Get("file")
data, _ := ioutil.ReadFile(filename)
data, _ := os.ReadFile(filename)
// Infer the Content-Type of the file.
contentType := http.DetectContentType(data[:512])

View file

@ -4,7 +4,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"strings"
)
@ -15,7 +14,7 @@ func main() {
tee := io.TeeReader(r, &buf)
printall := func(r io.Reader) {
b, err := ioutil.ReadAll(r)
b, err := io.ReadAll(r)
if err != nil {
log.Fatal(err)
}