-
Notifications
You must be signed in to change notification settings - Fork 2
/
example_test.go
46 lines (38 loc) · 911 Bytes
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package simplecache_test
import (
"fmt"
"image/png"
"log"
"github.com/schorlet/simplecache"
)
// Example gets an entry from the cache and prints it to stdout.
func Example() {
entry, err := simplecache.Get("https://golang.org/doc/gopher/pkg.png", "testdata")
if err != nil {
log.Fatal(err)
}
fmt.Println(entry.URL)
header, err := entry.Header()
if err != nil {
log.Fatal(err)
}
for _, key := range []string{"Status", "Content-Length", "Content-Type"} {
fmt.Printf("%s: %s\n", key, header.Get(key))
}
body, err := entry.Body()
if err != nil {
log.Fatal(err)
}
defer body.Close()
config, err := png.DecodeConfig(body)
if err != nil {
log.Fatal(err)
}
fmt.Printf("PNG image data, %d x %d\n", config.Width, config.Height)
// Output:
// https://golang.org/doc/gopher/pkg.png
// Status: 200
// Content-Length: 5409
// Content-Type: image/png
// PNG image data, 83 x 120
}