Skip to content

Commit ad9cea3

Browse files
committed
Tweak interface example type assertion + update link
1 parent 858d475 commit ad9cea3

File tree

4 files changed

+22
-36
lines changed

4 files changed

+22
-36
lines changed

examples/interfaces/interfaces.go

+5-8
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ func measure(g geometry) {
5151
fmt.Println(g.perim())
5252
}
5353

54-
// Type assertion can be performed to explicitly check the runtime type of the value.
55-
// It allows the access of fields and methods belonging to the specific type.
56-
// See [`switch` example](switch) for an alternative approach to handle type assertion.
54+
// Sometimes it's useful to know the runtime type of an
55+
// interface value. One option is using a *type assertion*
56+
// as shown here; another is a [type `switch`](switch).
5757
func detectCircle(g geometry) {
5858
if c, ok := g.(circle); ok {
59-
fmt.Println(c.radius)
59+
fmt.Println("circle with radius", c.radius)
6060
}
6161
}
6262

@@ -71,9 +71,6 @@ func main() {
7171
measure(r)
7272
measure(c)
7373

74-
// `detectCircle` takes structs that satisfy the `geometry` interface
75-
// if the struct is of type `circle`, it prints out the radius.
76-
detectCircle(r) // doesn't print anything.
74+
detectCircle(r)
7775
detectCircle(c)
78-
7976
}

examples/interfaces/interfaces.hash

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
9a362e2c9aed98013fa9b91af81d6cc373979db6
2-
tfsLP7R0dtH
1+
6324a4bdb756a0ec2ccc60e13c97d2650e730ed6
2+
xAAbgd7GOKD

examples/interfaces/interfaces.sh

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ $ go run interfaces.go
55
{5}
66
78.53981633974483
77
31.41592653589793
8-
5
8+
circle with radius 5
99

10-
# To learn more about Go's interfaces, check out this
11-
# [great blog post](https://jordanorelli.tumblr.com/post/32665860244/how-to-use-interfaces-in-go).
10+
# To understand how Go's interfaces work under the hood,
11+
# check out this [blog post](https://research.swtch.com/interfaces).

public/interfaces

+12-23
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)