Skip to content

Commit 8e63d9f

Browse files
Merge branch 'master' of https://github.com/ardanlabs/gotraining
2 parents 44db73a + 7e26abb commit 8e63d9f

File tree

3 files changed

+22
-22
lines changed

3 files changed

+22
-22
lines changed

topics/go/language/interfaces/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ _"The empty interface says nothing." - Rob Pike
3535
[Method Sets](example2/example2.go) ([Go Playground](https://play.golang.org/p/N50ocjUekf3))
3636
[Address Of Value](example3/example3.go) ([Go Playground](https://play.golang.org/p/w981JSUcVZ2))
3737
[Storage By Value](example4/example4.go) ([Go Playground](https://play.golang.org/p/6U232Ue_BY0))
38-
[Type Assertions](example5/example5.go) ([Go Playground](https://play.golang.org/p/YPBuhSxK6Ia))
39-
[Conditional Type Assertions](example6/example6.go) ([Go Playground](https://play.golang.org/p/Wwyg9dNYkj_l))
38+
[Type Assertions](example5/example5.go) ([Go Playground](https://play.golang.org/p/f47JMTj2eId))
39+
[Conditional Type Assertions](example6/example6.go) ([Go Playground](https://play.golang.org/p/9fYc5RyyvVG))
4040
[The Empty Interface and Type Switches](example7/example7.go) ([Go Playground](https://play.golang.org/p/iyDfKCIQ4S9))
4141

4242
## Advanced Code Review

topics/go/language/interfaces/example5/example5.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,22 @@ type finder interface {
2020
find(id int) (*user, error)
2121
}
2222

23-
// userDB defines a database we will access.
24-
type userDB struct {
23+
// userSVC is a service for dealing with users.
24+
type userSVC struct {
2525
host string
2626
}
2727

2828
// find implements the finder interface using pointer semantics.
29-
func (db *userDB) find(id int) (*user, error) {
29+
func (*userSVC) find(id int) (*user, error) {
3030
return &user{id: id, name: "Anna Walker"}, nil
3131
}
3232

3333
func main() {
34-
db := userDB{
34+
svc := userSVC{
3535
host: "localhost:3434",
3636
}
3737

38-
if err := run(&db); err != nil {
38+
if err := run(&svc); err != nil {
3939
log.Fatal(err)
4040
}
4141
}
@@ -54,16 +54,16 @@ func run(f finder) error {
5454
// you really need to get to the concrete value stored inside
5555
// the interface?
5656

57-
// Can you access the "host" field from the concrete userDB type pointer
57+
// Can you access the "host" field from the concrete userSVC type pointer
5858
// that is stored inside this interface variable? No, not directly.
5959
// All you know is the data has a method named "find".
6060
// ./example5.go:61:26: f.host undefined (type finder has no field or method host)
6161
log.Println("queried", f.host)
6262

63-
// You can use a type assertion to get a copy of the userDB pointer
63+
// You can use a type assertion to get a copy of the userSVC pointer
6464
// that is stored inside the interface.
65-
db := f.(*userDB)
66-
log.Println("queried", db.host)
65+
svc := f.(*userSVC)
66+
log.Println("queried", svc.host)
6767

6868
return nil
6969
}

topics/go/language/interfaces/example6/example6.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,28 @@ type finder interface {
2020
find(id int) (*user, error)
2121
}
2222

23-
// userDB defines a database we will access.
24-
type userDB struct {
23+
// userSVC is a service for dealing with users.
24+
type userSVC struct {
2525
host string
2626
}
2727

2828
// find implements the finder interface using pointer semantics.
29-
func (db *userDB) find(id int) (*user, error) {
29+
func (*userSVC) find(id int) (*user, error) {
3030
return &user{id: id, name: "Anna Walker"}, nil
3131
}
3232

33-
// mockDB defines a mock database we will access.
34-
type mockDB struct{}
33+
// mockSVC defines a mock service we will access.
34+
type mockSVC struct{}
3535

3636
// find implements the finder interface using pointer semantics.
37-
func (db *mockDB) find(id int) (*user, error) {
37+
func (*mockSVC) find(id int) (*user, error) {
3838
return &user{id: id, name: "Jacob Walker"}, nil
3939
}
4040

4141
func main() {
42-
var db mockDB
42+
var svc mockSVC
4343

44-
if err := run(&db); err != nil {
44+
if err := run(&svc); err != nil {
4545
log.Fatal(err)
4646
}
4747
}
@@ -54,10 +54,10 @@ func run(f finder) error {
5454
fmt.Printf("Found user %+v\n", u)
5555

5656
// If the concrete type value stored inside the interface value is of the
57-
// type *userDB, then "ok" will be true and "db" will be a copy of the
57+
// type *userSVC, then "ok" will be true and "svc" will be a copy of the
5858
// pointer stored inside the interface.
59-
if db, ok := f.(*userDB); ok {
60-
log.Println("queried", db.host)
59+
if svc, ok := f.(*userSVC); ok {
60+
log.Println("queried", svc.host)
6161
}
6262

6363
return nil

0 commit comments

Comments
 (0)