From 17359b7fb42a96a5e405d7a14335752a866b5ea7 Mon Sep 17 00:00:00 2001 From: Kevin Ho Date: Fri, 3 Jan 2025 15:14:28 -0800 Subject: [PATCH] added type assertion example --- examples/interfaces/interfaces.go | 14 ++++++++++ examples/interfaces/interfaces.hash | 4 +-- examples/interfaces/interfaces.sh | 1 + public/interfaces | 41 +++++++++++++++++++++++++---- 4 files changed, 53 insertions(+), 7 deletions(-) diff --git a/examples/interfaces/interfaces.go b/examples/interfaces/interfaces.go index 806ffa7d4..e5e00b805 100644 --- a/examples/interfaces/interfaces.go +++ b/examples/interfaces/interfaces.go @@ -41,6 +41,12 @@ func (c circle) perim() float64 { return 2 * math.Pi * c.radius } +// `circle` has a method called `circum` not part of `geometry` interface. +// `circum` is an alias for `perim` +func (c circle) circum() float64 { + return c.perim() +} + // If a variable has an interface type, then we can call // methods that are in the named interface. Here's a // generic `measure` function taking advantage of this @@ -61,4 +67,12 @@ func main() { // these structs as arguments to `measure`. measure(r) measure(c) + + // Type assertion can be performed + // to access methods not part of the `geometry` interface + var shape geometry + shape = circle{radius: 6} + if c, ok := shape.(circle); ok { + fmt.Println(c.circum()) + } } diff --git a/examples/interfaces/interfaces.hash b/examples/interfaces/interfaces.hash index b228f689a..0822ae0bd 100644 --- a/examples/interfaces/interfaces.hash +++ b/examples/interfaces/interfaces.hash @@ -1,2 +1,2 @@ -d4ea9541521cfee94107ba9331d0dabb1f9f16c1 -XJASG4MxBQr +f536687ba84047d88affba161c521d72b3759627 +iRa1sADCdAw diff --git a/examples/interfaces/interfaces.sh b/examples/interfaces/interfaces.sh index b71147638..452a91e76 100644 --- a/examples/interfaces/interfaces.sh +++ b/examples/interfaces/interfaces.sh @@ -5,6 +5,7 @@ $ go run interfaces.go {5} 78.53981633974483 31.41592653589793 +37.69911184307752 # To learn more about Go's interfaces, check out this # [great blog post](https://jordanorelli.tumblr.com/post/32665860244/how-to-use-interfaces-in-go). diff --git a/public/interfaces b/public/interfaces index 15a381113..1b48e98f5 100644 --- a/public/interfaces +++ b/public/interfaces @@ -45,7 +45,7 @@ signatures.

- +
package main
@@ -128,6 +128,20 @@ implement geometry on rects.

+ + +

circle has a method called circum not part of geometry interface. +circum is an alias for perim

+ + + + +
func (c circle) circum() float64 {
+    return c.perim()
+}
+ + +

If a variable has an interface type, then we can call @@ -166,10 +180,26 @@ instances of these structs as arguments to measure.

- +
    measure(r)
-    measure(c)
+    measure(c)
+ + + + + +

Type assertion can be performed +to access methods not part of the geometry interface

+ + + + +
    var shape geometry
+    shape = circle{radius: 6}
+    if c, ok := shape.(circle); ok {
+        fmt.Println(c.circum())
+    }
 }
@@ -190,7 +220,8 @@ these structs as arguments to measure.

14 {5} 78.53981633974483 -31.41592653589793 +31.41592653589793 +37.69911184307752 @@ -221,7 +252,7 @@ these structs as arguments to measure.