Skip to content

Commit 53acfd1

Browse files
Sections re-numbered
1 parent 76df812 commit 53acfd1

18 files changed

Lines changed: 188 additions & 130 deletions

File tree

‎chapter5/listing11/listing11.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ func main() {
3838

3939
// Values of type user can be used to call methods
4040
// declared with a pointer receiver.
41-
bill.changeEmail("bill@gmail.com")
41+
bill.changeEmail("bill@newdomain.com")
4242
bill.notify()
4343

4444
// Pointers of type user can be used to call methods
4545
// declared with a pointer receiver.
46-
lisa.changeEmail("lisa@gmail.com")
46+
lisa.changeEmail("lisa@newdomain.com")
4747
lisa.notify()
4848
}

‎chapter5/listing34/listing34.go‎

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,34 @@
1-
// Sample program to show how to use an interface in Go.
1+
// Sample program to show how to write a simple version of curl using
2+
// the io.Reader and io.Writer interface support.
23
package main
34

45
import (
56
"fmt"
7+
"io"
8+
"net/http"
9+
"os"
610
)
711

8-
// notifier is an interface that defined notification
9-
// type behavior.
10-
type notifier interface {
11-
notify()
12-
}
13-
14-
// user defines a user in the program.
15-
type user struct {
16-
name string
17-
email string
18-
}
19-
20-
// notify implements a method with a pointer receiver.
21-
func (u *user) notify() {
22-
fmt.Printf("Sending User Email To %s<%s>\n",
23-
u.name,
24-
u.email)
12+
// init is called before main.
13+
func init() {
14+
if len(os.Args) != 2 {
15+
fmt.Println("Usage: ./example2 <url>")
16+
os.Exit(-1)
17+
}
2518
}
2619

2720
// main is the entry point for the application.
2821
func main() {
29-
// Create a value of type User and send a notification.
30-
u := user{"Bill", "bill@email.com"}
31-
32-
sendNotification(u)
33-
34-
// ./listing34.go:32: cannot use u (type user) as type
35-
// notifier in argument to sendNotification:
36-
// user does not implement notifier
37-
// (notify method has pointer receiver)
38-
}
39-
40-
// sendNotification accepts values that implement the notifier
41-
// interface and sends notifications.
42-
func sendNotification(n notifier) {
43-
n.notify()
22+
// Get a response from the web server.
23+
r, err := http.Get(os.Args[1])
24+
if err != nil {
25+
fmt.Println(err)
26+
return
27+
}
28+
29+
// Copies from the Body to Stdout.
30+
io.Copy(os.Stdout, r.Body)
31+
if err := r.Body.Close(); err != nil {
32+
fmt.Println(err)
33+
}
4434
}

‎chapter5/listing35/listing35.go‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Sample program to show how a bytes.Buffer can also be used
2+
// with the io.Copy function.
3+
package main
4+
5+
import (
6+
"bytes"
7+
"fmt"
8+
"io"
9+
"os"
10+
)
11+
12+
// main is the entry point for the application.
13+
func main() {
14+
var b bytes.Buffer
15+
16+
// Write a string to the buffer.
17+
b.Write([]byte("Hello"))
18+
19+
// Use Fprintf to concatenate a string to the Buffer.
20+
fmt.Fprintf(&b, "World!")
21+
22+
// Write the content of the Buffer to stdout.
23+
io.Copy(os.Stdout, &b)
24+
}

‎chapter5/listing36/listing36.go‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Sample program to show how to use an interface in Go.
2+
package main
3+
4+
import (
5+
"fmt"
6+
)
7+
8+
// notifier is an interface that defined notification
9+
// type behavior.
10+
type notifier interface {
11+
notify()
12+
}
13+
14+
// user defines a user in the program.
15+
type user struct {
16+
name string
17+
email string
18+
}
19+
20+
// notify implements a method with a pointer receiver.
21+
func (u *user) notify() {
22+
fmt.Printf("Sending User Email To %s<%s>\n",
23+
u.name,
24+
u.email)
25+
}
26+
27+
// main is the entry point for the application.
28+
func main() {
29+
// Create a value of type User and send a notification.
30+
u := user{"Bill", "bill@email.com"}
31+
32+
sendNotification(u)
33+
34+
// ./listing36.go:32: cannot use u (type user) as type
35+
// notifier in argument to sendNotification:
36+
// user does not implement notifier
37+
// (notify method has pointer receiver)
38+
}
39+
40+
// sendNotification accepts values that implement the notifier
41+
// interface and sends notifications.
42+
func sendNotification(n notifier) {
43+
n.notify()
44+
}

‎chapter5/listing44/listing44.go‎

Lines changed: 0 additions & 21 deletions
This file was deleted.

‎chapter5/listing46/listing46.go‎

Lines changed: 11 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,21 @@
1-
// Sample program to show how polymorphic behavior with interfaces.
1+
// Sample program to show how you can't always get the
2+
// address of a value.
23
package main
34

4-
import (
5-
"fmt"
6-
)
5+
import "fmt"
76

8-
// notifier is an interface that defines notification
9-
// type behavior.
10-
type notifier interface {
11-
notify()
12-
}
13-
14-
// user defines a user in the program.
15-
type user struct {
16-
name string
17-
email string
18-
}
19-
20-
// notify implements the notifier interface with a pointer receiver.
21-
func (u *user) notify() {
22-
fmt.Printf("Sending user Email To %s<%s>\n",
23-
u.name,
24-
u.email)
25-
}
7+
// duration is a named type with a base type of int.
8+
type duration int
269

27-
// admin defines a admin in the program.
28-
type admin struct {
29-
name string
30-
email string
31-
}
32-
33-
// notify implements the notifier interface with a pointer receiver.
34-
func (a *admin) notify() {
35-
fmt.Printf("Sending admin Email To %s<%s>\n",
36-
a.name,
37-
a.email)
10+
// format pretty-prints the duration value.
11+
func (d *duration) pretty() string {
12+
return fmt.Sprintf("Duration: %d", *d)
3813
}
3914

4015
// main is the entry point for the application.
4116
func main() {
42-
// Create a user value and pass it to sendNotification.
43-
bill := user{"Bill", "bill@email.com"}
44-
sendNotification(&bill)
45-
46-
// Create an admin value and pass it to sendNotification.
47-
jill := admin{"Jill", "jill@email.com"}
48-
sendNotification(&jill)
49-
}
17+
duration(42).pretty()
5018

51-
// sendNotification accepts values that implement the notifier
52-
// interface and sends notifications.
53-
func sendNotification(n notifier) {
54-
n.notify()
19+
// ./listing46.go:17: cannot call pointer method on duration(42)
20+
// ./listing46.go:17: cannot take the address of duration(42)
5521
}

‎chapter5/listing48/listing48.go‎

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,55 @@
1-
// Sample program to show how to embed a type into another type and
2-
// the relationship between the inner and outer type.
1+
// Sample program to show how polymorphic behavior with interfaces.
32
package main
43

54
import (
65
"fmt"
76
)
87

8+
// notifier is an interface that defines notification
9+
// type behavior.
10+
type notifier interface {
11+
notify()
12+
}
13+
914
// user defines a user in the program.
1015
type user struct {
1116
name string
1217
email string
1318
}
1419

15-
// notify implements a method that can be called via
16-
// a value of type user.
20+
// notify implements the notifier interface with a pointer receiver.
1721
func (u *user) notify() {
18-
fmt.Printf("Sending user email To %s<%s>\n",
22+
fmt.Printf("Sending user Email To %s<%s>\n",
1923
u.name,
2024
u.email)
2125
}
2226

23-
// admin represents an admin user with privileges.
27+
// admin defines a admin in the program.
2428
type admin struct {
25-
user // Embedded Type
26-
level string
29+
name string
30+
email string
31+
}
32+
33+
// notify implements the notifier interface with a pointer receiver.
34+
func (a *admin) notify() {
35+
fmt.Printf("Sending admin Email To %s<%s>\n",
36+
a.name,
37+
a.email)
2738
}
2839

2940
// main is the entry point for the application.
3041
func main() {
31-
// Create an admin user.
32-
ad := admin{
33-
user: user{
34-
name: "john smith",
35-
email: "john@yahoo.com",
36-
},
37-
level: "super",
38-
}
39-
40-
// We can access the inner type's method directly.
41-
ad.user.notify()
42-
43-
// The inner type's method is promoted.
44-
ad.notify()
42+
// Create a user value and pass it to sendNotification.
43+
bill := user{"Bill", "bill@email.com"}
44+
sendNotification(&bill)
45+
46+
// Create an admin value and pass it to sendNotification.
47+
jill := admin{"Jill", "jill@email.com"}
48+
sendNotification(&jill)
49+
}
50+
51+
// sendNotification accepts values that implement the notifier
52+
// interface and sends notifications.
53+
func sendNotification(n notifier) {
54+
n.notify()
4555
}

‎chapter5/listing50/listing50.go‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Sample program to show how to embed a type into another type and
2+
// the relationship between the inner and outer type.
3+
package main
4+
5+
import (
6+
"fmt"
7+
)
8+
9+
// user defines a user in the program.
10+
type user struct {
11+
name string
12+
email string
13+
}
14+
15+
// notify implements a method that can be called via
16+
// a value of type user.
17+
func (u *user) notify() {
18+
fmt.Printf("Sending user email To %s<%s>\n",
19+
u.name,
20+
u.email)
21+
}
22+
23+
// admin represents an admin user with privileges.
24+
type admin struct {
25+
user // Embedded Type
26+
level string
27+
}
28+
29+
// main is the entry point for the application.
30+
func main() {
31+
// Create an admin user.
32+
ad := admin{
33+
user: user{
34+
name: "john smith",
35+
email: "john@yahoo.com",
36+
},
37+
level: "super",
38+
}
39+
40+
// We can access the inner type's method directly.
41+
ad.user.notify()
42+
43+
// The inner type's method is promoted.
44+
ad.notify()
45+
}

0 commit comments

Comments
 (0)