added example for naked return
This commit is contained in:
parent
30a235b655
commit
0cf5cd7a93
@ -12,6 +12,16 @@ func vals() (int, int) {
|
|||||||
return 3, 7
|
return 3, 7
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Go's return values may be named. If so, they are treated
|
||||||
|
// as variables defined at the top of the function.
|
||||||
|
// A `return“ statement without arguments returns the
|
||||||
|
// named return values. This is known as a "naked" return.
|
||||||
|
func split(sum int) (x, y int) {
|
||||||
|
x = sum * 4 / 9
|
||||||
|
y = sum - x
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
// Here we use the 2 different return values from the
|
// Here we use the 2 different return values from the
|
||||||
@ -24,4 +34,8 @@ func main() {
|
|||||||
// use the blank identifier `_`.
|
// use the blank identifier `_`.
|
||||||
_, c := vals()
|
_, c := vals()
|
||||||
fmt.Println(c)
|
fmt.Println(c)
|
||||||
|
|
||||||
|
// The split function will return the values of x & y
|
||||||
|
d, e := split(17)
|
||||||
|
fmt.Println(d, e)
|
||||||
}
|
}
|
||||||
|
@ -1,2 +1,2 @@
|
|||||||
c6e4f5dd9c55b5d2aaeb7e939c216ec76f042501
|
57e7f6308442d1364b13c9217eae8f934a72b408
|
||||||
vZdUvLB1WbK
|
U87Tk2E0TIt
|
||||||
|
@ -2,6 +2,11 @@ $ go run multiple-return-values.go
|
|||||||
3
|
3
|
||||||
7
|
7
|
||||||
7
|
7
|
||||||
|
7 10
|
||||||
|
|
||||||
|
# Naked return statements should be used only in short
|
||||||
|
# functions.
|
||||||
|
# They can harm readability in longer functions.
|
||||||
|
|
||||||
# Accepting a variable number of arguments is another nice
|
# Accepting a variable number of arguments is another nice
|
||||||
# feature of Go functions; we'll look at this next.
|
# feature of Go functions; we'll look at this next.
|
||||||
|
56
public/multiple-return-values
generated
56
public/multiple-return-values
generated
@ -43,7 +43,7 @@ to return both result and error values from a function.</p>
|
|||||||
|
|
||||||
</td>
|
</td>
|
||||||
<td class="code leading">
|
<td class="code leading">
|
||||||
<a href="https://go.dev/play/p/vZdUvLB1WbK"><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/U87Tk2E0TIt"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||||
<pre class="chroma"><span class="kn">package</span> <span class="nx">main</span>
|
<pre class="chroma"><span class="kn">package</span> <span class="nx">main</span>
|
||||||
</pre>
|
</pre>
|
||||||
</td>
|
</td>
|
||||||
@ -76,6 +76,26 @@ the function returns 2 <code>int</code>s.</p>
|
|||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td class="docs">
|
||||||
|
<p>Go’s return values may be named. If so, they are treated
|
||||||
|
as variables defined at the top of the function.
|
||||||
|
A `return“ statement without arguments returns the
|
||||||
|
named return values. This is known as a “naked” return.</p>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
<td class="code leading">
|
||||||
|
|
||||||
|
<pre class="chroma">
|
||||||
|
<span class="kd">func</span> <span class="nf">split</span><span class="p">(</span><span class="nx">sum</span> <span class="kt">int</span><span class="p">)</span> <span class="p">(</span><span class="nx">x</span><span class="p">,</span> <span class="nx">y</span> <span class="kt">int</span><span class="p">)</span> <span class="p">{</span>
|
||||||
|
<span class="nx">x</span> <span class="p">=</span> <span class="nx">sum</span> <span class="o">*</span> <span class="mi">4</span> <span class="o">/</span> <span class="mi">9</span>
|
||||||
|
<span class="nx">y</span> <span class="p">=</span> <span class="nx">sum</span> <span class="o">-</span> <span class="nx">x</span>
|
||||||
|
<span class="k">return</span>
|
||||||
|
<span class="p">}</span>
|
||||||
|
</pre>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
<tr>
|
<tr>
|
||||||
<td class="docs">
|
<td class="docs">
|
||||||
|
|
||||||
@ -109,11 +129,25 @@ call with <em>multiple assignment</em>.</p>
|
|||||||
use the blank identifier <code>_</code>.</p>
|
use the blank identifier <code>_</code>.</p>
|
||||||
|
|
||||||
</td>
|
</td>
|
||||||
<td class="code">
|
<td class="code leading">
|
||||||
|
|
||||||
<pre class="chroma">
|
<pre class="chroma">
|
||||||
<span class="nx">_</span><span class="p">,</span> <span class="nx">c</span> <span class="o">:=</span> <span class="nf">vals</span><span class="p">()</span>
|
<span class="nx">_</span><span class="p">,</span> <span class="nx">c</span> <span class="o">:=</span> <span class="nf">vals</span><span class="p">()</span>
|
||||||
<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">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="nx">c</span><span class="p">)</span>
|
||||||
|
</pre>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td class="docs">
|
||||||
|
<p>The split function will return the values of x & y</p>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
<td class="code">
|
||||||
|
|
||||||
|
<pre class="chroma">
|
||||||
|
<span class="nx">d</span><span class="p">,</span> <span class="nx">e</span> <span class="o">:=</span> <span class="nf">split</span><span class="p">(</span><span class="mi">17</span><span class="p">)</span>
|
||||||
|
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="nx">d</span><span class="p">,</span> <span class="nx">e</span><span class="p">)</span>
|
||||||
<span class="p">}</span>
|
<span class="p">}</span>
|
||||||
</pre>
|
</pre>
|
||||||
</td>
|
</td>
|
||||||
@ -132,7 +166,21 @@ use the blank identifier <code>_</code>.</p>
|
|||||||
<pre class="chroma"><span class="gp">$</span> go run multiple-return-values.go
|
<pre class="chroma"><span class="gp">$</span> go run multiple-return-values.go
|
||||||
<span class="go">3
|
<span class="go">3
|
||||||
</span><span class="go">7
|
</span><span class="go">7
|
||||||
</span><span class="go">7</span></pre>
|
</span><span class="go">7
|
||||||
|
</span><span class="go">7 10</span></pre>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td class="docs">
|
||||||
|
<p>Naked return statements should be used only in short
|
||||||
|
functions.
|
||||||
|
They can harm readability in longer functions.</p>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
<td class="code empty leading">
|
||||||
|
|
||||||
|
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
|
||||||
@ -163,7 +211,7 @@ feature of Go functions; we’ll look at this next.</p>
|
|||||||
</div>
|
</div>
|
||||||
<script>
|
<script>
|
||||||
var codeLines = [];
|
var codeLines = [];
|
||||||
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('func vals() (int, int) {\u000A return 3, 7\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' a, b :\u003D vals()\u000A fmt.Println(a)\u000A fmt.Println(b)\u000A');codeLines.push(' _, c :\u003D vals()\u000A fmt.Println(c)\u000A}\u000A');codeLines.push('');codeLines.push('');
|
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('func vals() (int, int) {\u000A return 3, 7\u000A}\u000A');codeLines.push('func split(sum int) (x, y int) {\u000A x \u003D sum * 4 / 9\u000A y \u003D sum - x\u000A return\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' a, b :\u003D vals()\u000A fmt.Println(a)\u000A fmt.Println(b)\u000A');codeLines.push(' _, c :\u003D vals()\u000A fmt.Println(c)\u000A');codeLines.push(' d, e :\u003D split(17)\u000A fmt.Println(d, e)\u000A}\u000A');codeLines.push('');codeLines.push('');codeLines.push('');
|
||||||
</script>
|
</script>
|
||||||
<script src="site.js" async></script>
|
<script src="site.js" async></script>
|
||||||
</body>
|
</body>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user