[lectures] Update io slides.
This commit is contained in:
parent
f4feebffbe
commit
edd97e0f1b
5 changed files with 17 additions and 16 deletions
|
@ -2,10 +2,9 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
_, _ = io.Copy(ioutil.Discard, strings.NewReader("nothing of use"))
|
_, _ = io.Copy(io.Discard, strings.NewReader("nothing of use"))
|
||||||
}
|
}
|
||||||
|
|
|
@ -122,7 +122,7 @@ Problem: proxy a chunked HTTP in a stream.
|
||||||
|
|
||||||
.play -edit httpchunking/solution3/main.go /^func transfer/,/^}/
|
.play -edit httpchunking/solution3/main.go /^func transfer/,/^}/
|
||||||
|
|
||||||
* ioutil
|
* ioutil (deprecated)
|
||||||
|
|
||||||
Package *io/ioutil* implements some I/O utility functions.
|
Package *io/ioutil* implements some I/O utility functions.
|
||||||
|
|
||||||
|
@ -216,7 +216,7 @@ Can we avoid using an intermediate buffer entirely?
|
||||||
|
|
||||||
* Example: sendfile
|
* 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
|
// ReadFrom is here to optimize copying from an *os.File regular file
|
||||||
// to a *net.TCPConn with sendfile.
|
// 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) {
|
func (c *TCPConn) ReadFrom(r io.Reader) (int64, error) {
|
||||||
|
|
||||||
* ioutil.Discard
|
* io.Discard
|
||||||
|
|
||||||
// Discard is an io.Writer on which all Write calls succeed
|
var Discard Writer = discard{}
|
||||||
// without doing anything.
|
|
||||||
var Discard io.Writer = devNull(0)
|
type discard struct{}
|
||||||
|
|
||||||
|
func (discard) Write(p []byte) (int, error) {
|
||||||
|
return len(p), nil
|
||||||
|
}
|
||||||
|
|
||||||
- Implements io.ReaderFrom!
|
- 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:])
|
return string(b.buf[b.off:])
|
||||||
}
|
}
|
||||||
|
|
||||||
* strings.Builder vs strings.Builder
|
* bytes.Buffer vs strings.Builder
|
||||||
|
|
||||||
.play -edit stringsbuilder/main.go
|
.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
|
- useful if you don't want to read the whole file into memory
|
||||||
- has no internal buffers
|
- 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)
|
- allocates a byte slice of the correct size (no need to Read + append in a loop)
|
||||||
- closes the file
|
- closes the file
|
||||||
|
|
|
@ -2,7 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
@ -11,7 +11,7 @@ func main() {
|
||||||
r := strings.NewReader("Go is a general-purpose language " +
|
r := strings.NewReader("Go is a general-purpose language " +
|
||||||
"designed with systems programming in mind.")
|
"designed with systems programming in mind.")
|
||||||
|
|
||||||
b, err := ioutil.ReadAll(r)
|
b, err := io.ReadAll(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
@ -18,7 +17,7 @@ func main() {
|
||||||
|
|
||||||
func readFileHandler(w http.ResponseWriter, r *http.Request) {
|
func readFileHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
filename := r.URL.Query().Get("file")
|
filename := r.URL.Query().Get("file")
|
||||||
data, _ := ioutil.ReadFile(filename)
|
data, _ := os.ReadFile(filename)
|
||||||
|
|
||||||
// Infer the Content-Type of the file.
|
// Infer the Content-Type of the file.
|
||||||
contentType := http.DetectContentType(data[:512])
|
contentType := http.DetectContentType(data[:512])
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
@ -15,7 +14,7 @@ func main() {
|
||||||
tee := io.TeeReader(r, &buf)
|
tee := io.TeeReader(r, &buf)
|
||||||
|
|
||||||
printall := func(r io.Reader) {
|
printall := func(r io.Reader) {
|
||||||
b, err := ioutil.ReadAll(r)
|
b, err := io.ReadAll(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue