refactor: handle err with fprintln io compose ifaces
This commit is contained in:
4
interfaces/16-io-compose/.gitignore
vendored
4
interfaces/16-io-compose/.gitignore
vendored
@ -1,2 +1,4 @@
|
|||||||
rosie.unknown
|
rosie.unknown
|
||||||
rosie.png
|
rosie.png
|
||||||
|
_other.go
|
||||||
|
toc
|
@ -13,7 +13,6 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
@ -21,25 +20,23 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
resp, err := http.Get("https://inancgumus.github.com/x/rosie.unknown")
|
resp, err := http.Get("https://inancgumus.github.com/x/rosie.unknown")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
fmt.Fprintln(os.Stderr, err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
// creates a file (or truncates if the file exists)
|
// creates a file (or truncates if the file exists)
|
||||||
file, err := os.Create("rosie.png")
|
file, err := os.Create("rosie.png")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
fmt.Fprintln(os.Stderr, err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
n, err := transfer(resp.Body, file)
|
n, err := transfer(resp.Body, file)
|
||||||
|
|
||||||
// check whether the error was due to the PNG signature.
|
|
||||||
if errors.Is(err, errNotPNG) {
|
|
||||||
log.Fatalln("Please provide a PNG image:", err)
|
|
||||||
}
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
fmt.Fprintln(os.Stderr, err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
fmt.Printf("%d bytes transferred.\n", n)
|
fmt.Printf("%d bytes transferred.\n", n)
|
||||||
|
|
||||||
@ -48,10 +45,6 @@ func main() {
|
|||||||
// resp.Body.Close()
|
// resp.Body.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
// create an error at the package-level.
|
|
||||||
// so the other funcs in the package can check.
|
|
||||||
var errNotPNG = errors.New("not a png image")
|
|
||||||
|
|
||||||
// transfer streams data from a reader to a writer.
|
// transfer streams data from a reader to a writer.
|
||||||
// if the reader doesn't contain a PNG signature, transfer returns errNotPNG.
|
// if the reader doesn't contain a PNG signature, transfer returns errNotPNG.
|
||||||
// otherwise, it transfers the data to the writer.
|
// otherwise, it transfers the data to the writer.
|
||||||
@ -74,7 +67,7 @@ func transfer(r io.Reader, w io.Writer) (n int64, err error) {
|
|||||||
|
|
||||||
// check the png signature.
|
// check the png signature.
|
||||||
if !bytes.HasPrefix(memory.Bytes(), []byte(pngSign)) {
|
if !bytes.HasPrefix(memory.Bytes(), []byte(pngSign)) {
|
||||||
return n, errNotPNG
|
return n, errors.New("not png")
|
||||||
}
|
}
|
||||||
|
|
||||||
// stitch the PNG signature (memory) and the response body reader together.
|
// stitch the PNG signature (memory) and the response body reader together.
|
||||||
|
Reference in New Issue
Block a user