Tweak interface example type assertion + update link

This commit is contained in:
Eli Bendersky
2025-01-06 05:47:45 -08:00
parent 858d475416
commit ad9cea3a54
4 changed files with 22 additions and 36 deletions

View File

@@ -51,12 +51,12 @@ func measure(g geometry) {
fmt.Println(g.perim())
}
// Type assertion can be performed to explicitly check the runtime type of the value.
// It allows the access of fields and methods belonging to the specific type.
// See [`switch` example](switch) for an alternative approach to handle type assertion.
// Sometimes it's useful to know the runtime type of an
// interface value. One option is using a *type assertion*
// as shown here; another is a [type `switch`](switch).
func detectCircle(g geometry) {
if c, ok := g.(circle); ok {
fmt.Println(c.radius)
fmt.Println("circle with radius", c.radius)
}
}
@@ -71,9 +71,6 @@ func main() {
measure(r)
measure(c)
// `detectCircle` takes structs that satisfy the `geometry` interface
// if the struct is of type `circle`, it prints out the radius.
detectCircle(r) // doesn't print anything.
detectCircle(r)
detectCircle(c)
}

View File

@@ -1,2 +1,2 @@
9a362e2c9aed98013fa9b91af81d6cc373979db6
tfsLP7R0dtH
6324a4bdb756a0ec2ccc60e13c97d2650e730ed6
xAAbgd7GOKD

View File

@@ -5,7 +5,7 @@ $ go run interfaces.go
{5}
78.53981633974483
31.41592653589793
5
circle with radius 5
# 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).
# To understand how Go's interfaces work under the hood,
# check out this [blog post](https://research.swtch.com/interfaces).

35
public/interfaces generated
View File

@@ -45,7 +45,7 @@ signatures.</p>
</td>
<td class="code leading">
<a href="https://go.dev/play/p/tfsLP7R0dtH"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
<a href="https://go.dev/play/p/xAAbgd7GOKD"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
<pre class="chroma"><code><span class="line"><span class="cl"><span class="kn">package</span> <span class="nx">main</span></span></span></code></pre>
</td>
</tr>
@@ -148,16 +148,16 @@ to work on any <code>geometry</code>.</p>
<tr>
<td class="docs">
<p>Type assertion can be performed to explicitly check the runtime type of the value.
It allows the access of fields and methods belonging to the specific type.
See <a href="switch"><code>switch</code> example</a> for an alternative approach to handle type assertion.</p>
<p>Sometimes it&rsquo;s useful to know the runtime type of an
interface value. One option is using a <em>type assertion</em>
as shown here; another is a <a href="switch">type <code>switch</code></a>.</p>
</td>
<td class="code leading">
<pre class="chroma"><code><span class="line"><span class="cl"><span class="kd">func</span> <span class="nf">detectCircle</span><span class="p">(</span><span class="nx">g</span> <span class="nx">geometry</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="k">if</span> <span class="nx">c</span><span class="p">,</span> <span class="nx">ok</span> <span class="o">:=</span> <span class="nx">g</span><span class="p">.(</span><span class="nx">circle</span><span class="p">);</span> <span class="nx">ok</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="nx">c</span><span class="p">.</span><span class="nx">radius</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"> <span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">&#34;circle with radius&#34;</span><span class="p">,</span> <span class="nx">c</span><span class="p">.</span><span class="nx">radius</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"> <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span></span></span></code></pre>
</td>
@@ -190,26 +190,15 @@ these structs as arguments to <code>measure</code>.</p>
</td>
</tr>
<tr>
<td class="docs">
<p><code>detectCircle</code> takes structs that satisfy the <code>geometry</code> interface
if the struct is of type <code>circle</code>, it prints out the radius.</p>
</td>
<td class="code leading">
<pre class="chroma"><code><span class="line"><span class="cl"> <span class="nf">detectCircle</span><span class="p">(</span><span class="nx">r</span><span class="p">)</span> <span class="c1">// doesn&#39;t print anything.
</span></span></span><span class="line"><span class="cl"><span class="c1"></span> <span class="nf">detectCircle</span><span class="p">(</span><span class="nx">c</span><span class="p">)</span></span></span></code></pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code">
<pre class="chroma"><code><span class="line"><span class="cl"><span class="p">}</span></span></span></code></pre>
<pre class="chroma"><code><span class="line"><span class="cl"> <span class="nf">detectCircle</span><span class="p">(</span><span class="nx">r</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"> <span class="nf">detectCircle</span><span class="p">(</span><span class="nx">c</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span></span></span></code></pre>
</td>
</tr>
@@ -230,14 +219,14 @@ if the struct is of type <code>circle</code>, it prints out the radius.</p>
</span></span></span><span class="line"><span class="cl"><span class="go">{5}
</span></span></span><span class="line"><span class="cl"><span class="go">78.53981633974483
</span></span></span><span class="line"><span class="cl"><span class="go">31.41592653589793
</span></span></span><span class="line"><span class="cl"><span class="go">5</span></span></span></code></pre>
</span></span></span><span class="line"><span class="cl"><span class="go">circle with radius 5</span></span></span></code></pre>
</td>
</tr>
<tr>
<td class="docs">
<p>To learn more about Go&rsquo;s interfaces, check out this
<a href="https://jordanorelli.tumblr.com/post/32665860244/how-to-use-interfaces-in-go">great blog post</a>.</p>
<p>To understand how Go&rsquo;s interfaces work under the hood,
check out this <a href="https://research.swtch.com/interfaces">blog post</a>.</p>
</td>
<td class="code empty">
@@ -261,7 +250,7 @@ if the struct is of type <code>circle</code>, it prints out the radius.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"math\"\u000A)\u000A');codeLines.push('type geometry interface {\u000A area() float64\u000A perim() float64\u000A}\u000A');codeLines.push('type rect struct {\u000A width, height float64\u000A}\u000Atype circle struct {\u000A radius float64\u000A}\u000A');codeLines.push('func (r rect) area() float64 {\u000A return r.width * r.height\u000A}\u000Afunc (r rect) perim() float64 {\u000A return 2*r.width + 2*r.height\u000A}\u000A');codeLines.push('func (c circle) area() float64 {\u000A return math.Pi * c.radius * c.radius\u000A}\u000Afunc (c circle) perim() float64 {\u000A return 2 * math.Pi * c.radius\u000A}\u000A');codeLines.push('func measure(g geometry) {\u000A fmt.Println(g)\u000A fmt.Println(g.area())\u000A fmt.Println(g.perim())\u000A}\u000A');codeLines.push('func detectCircle(g geometry) {\u000A if c, ok :\u003D g.(circle); ok {\u000A fmt.Println(c.radius)\u000A }\u000A}\u000A');codeLines.push('func main() {\u000A r :\u003D rect{width: 3, height: 4}\u000A c :\u003D circle{radius: 5}\u000A');codeLines.push(' measure(r)\u000A measure(c)\u000A');codeLines.push(' detectCircle(r) // doesn\'t print anything.\u000A detectCircle(c)\u000A');codeLines.push('}\u000A');codeLines.push('');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"math\"\u000A)\u000A');codeLines.push('type geometry interface {\u000A area() float64\u000A perim() float64\u000A}\u000A');codeLines.push('type rect struct {\u000A width, height float64\u000A}\u000Atype circle struct {\u000A radius float64\u000A}\u000A');codeLines.push('func (r rect) area() float64 {\u000A return r.width * r.height\u000A}\u000Afunc (r rect) perim() float64 {\u000A return 2*r.width + 2*r.height\u000A}\u000A');codeLines.push('func (c circle) area() float64 {\u000A return math.Pi * c.radius * c.radius\u000A}\u000Afunc (c circle) perim() float64 {\u000A return 2 * math.Pi * c.radius\u000A}\u000A');codeLines.push('func measure(g geometry) {\u000A fmt.Println(g)\u000A fmt.Println(g.area())\u000A fmt.Println(g.perim())\u000A}\u000A');codeLines.push('func detectCircle(g geometry) {\u000A if c, ok :\u003D g.(circle); ok {\u000A fmt.Println(\"circle with radius\", c.radius)\u000A }\u000A}\u000A');codeLines.push('func main() {\u000A r :\u003D rect{width: 3, height: 4}\u000A c :\u003D circle{radius: 5}\u000A');codeLines.push(' measure(r)\u000A measure(c)\u000A');codeLines.push(' detectCircle(r)\u000A detectCircle(c)\u000A}\u000A');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>