Skip to content

Commit b5838d0

Browse files
Updated code samples and numbers
1 parent 77285e8 commit b5838d0

4 files changed

Lines changed: 14 additions & 25 deletions

File tree

‎chapter7/listing03/listing03.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
)
77

88
func init() {
9-
log.SetPrefix("PREFIX: ")
9+
log.SetPrefix("TRACE: ")
1010
log.SetFlags(log.Ldate | log.Lmicroseconds | log.Llongfile)
1111
}
1212

‎chapter7/listing31/listing31.go‎

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// This sample program demonstrates how to decode a JSON string.
1+
// This sample program demonstrates how to marshal a JSON string.
22
package main
33

44
import (
@@ -7,26 +7,17 @@ import (
77
"log"
88
)
99

10-
// JSON contains a sample string to unmarshal.
11-
var JSON = `{
12-
"name": "Gopher",
13-
"title": "programmer",
14-
"contact": {
15-
"home": "415.333.3333",
16-
"cell": "415.555.5555"
17-
}
18-
}`
19-
2010
func main() {
21-
// Unmarshal the JSON string into our map variable.
22-
var c map[string]interface{}
23-
err := json.Unmarshal([]byte(JSON), &c)
24-
if err != nil {
25-
log.Println("ERROR:", err)
26-
return
11+
// Create a map of key/value pairs.
12+
c := make(map[string]interface{})
13+
c["name"] = "Gopher"
14+
c["title"] = "programmer"
15+
c["contact"] = map[string]interface{}{
16+
"home": "415.333.3333",
17+
"cell": "415.555.5555",
2718
}
2819

29-
// Marshal the map back into a JSON string.
20+
// Marshal the map into a JSON string.
3021
data, err := json.MarshalIndent(c, "", " ")
3122
if err != nil {
3223
log.Println("ERROR:", err)

‎chapter7/listing46/listing46.go‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
package main
44

55
import (
6-
"fmt"
76
"io"
7+
"log"
88
"net/http"
99
"os"
1010
)
@@ -14,15 +14,13 @@ func main() {
1414
// r here is a response, and r.Body is an io.Reader.
1515
r, err := http.Get(os.Args[1])
1616
if err != nil {
17-
fmt.Println(err)
18-
return
17+
log.Fatalln(err)
1918
}
2019

2120
// Create a file to persist the response.
2221
file, err := os.Create(os.Args[2])
2322
if err != nil {
24-
fmt.Println(err)
25-
return
23+
log.Fatalln(err)
2624
}
2725
defer file.Close()
2826

@@ -33,6 +31,6 @@ func main() {
3331
// Read the response and write to both destinations.
3432
io.Copy(dest, r.Body)
3533
if err := r.Body.Close(); err != nil {
36-
fmt.Println(err)
34+
log.Println(err)
3735
}
3836
}

0 commit comments

Comments
 (0)