October 2015
Beginner to intermediate
400 pages
14h 44m
English
The logic below is similar to the part of the net/http web
server responsible for writing HTTP header fields
such as "Content-type: text/html".
The io.Writer w represents the HTTP response; the bytes
written to it are ultimately sent to someone’s web browser.
func writeHeader(w io.Writer, contentType string) error {
if _, err := w.Write([]byte("Content-Type: ")); err != nil {
return err
}
if _, err := w.Write([]byte(contentType)); err != nil {
return err
}
// ...
}
Because the Write method requires a byte slice, and the value we
wish to write is a string, a []byte(...) conversion is required.
This conversion allocates memory and makes a copy, but the copy ...