<p>1- When and where to use Function with Multiple Returning Values?</p>
<p>2- Please correct my program:</p>
<p>package main</p>
<p>import "fmt"</p>
<p>func main() {</p>
<pre><code>var c = []int setNumbers(5, 6)
fmt.Println(c)
</code></pre>
<p>}</p>
<p>func setNumbers(a , b int) int {</p>
<pre><code>return a, b
</code></pre>
<p>}</p>
<hr/>**评论:**<br/><br/>natefinch: <pre><p>I can explain, but I think it's better to start by going through the tour of go, to get a handle on how go works: <a href="https://tour.golang.org/" rel="nofollow">https://tour.golang.org/</a></p>
<p>Honestly, the easiest way to make c into a slice of the ints 5 and 6 is to create the slice on that line:</p>
<pre><code>c := []int{ 5, 6 }
</code></pre>
<p>This creates a slice of two ints, with values 5 and 6, creates the variable c, and assigns that slice to c.</p>
<p>However, if you want setNumbers to return multiple values, you need to specify that it returns multiple values. Right now it is specified to return a single int.</p>
<pre><code>func setNumbers(a , b int) int {
</code></pre>
<p><sup>^</sup> This says "setNumber returns a single int"</p>
<p>If you want it to return two ints, you have to specify that in the function signature like this:</p>
<pre><code>func setNumbers(a , b int) (int, int) {
</code></pre>
<p>Note that if you have more than one return value, they have to be in parentheses. </p>
<p>Now, that will make setNumbers return two ints, but you can't use two return value to create a slice. Each return value has to be assigned to a separate variable where the function is called. There's no way to convert them into a slice (without passing them into a different function).</p>
<p>I hope that helps.</p></pre>baliance: <pre><p>Since the Go source tree is gofmt'd, you can easily find all of the functions returning multiple values with grep:</p>
<pre><code>grep -r 'func .* (.*,.*) {'
</code></pre>
<p>As of tip from a few weeks ago, there are around 55k functions, of which there are almost 5k functions that return multiple values. </p>
<ul>
<li> 1 function with 16 return values (test function for compiler)</li>
<li> 4 functions with 7 return values</li>
<li> 5 functions with 6 return values</li>
<li> 35 functions with 5 return values</li>
<li> 69 functions with 4 return values</li>
<li> 433 functions with 3 return values</li>
<li> 4202 functions with 2 return values</li>
</ul>
<p>Only five non-test functions return 5 or more values, and none of those are exported:</p>
<pre><code>pkg/bootstrap/src/bootstrap/cmd/compile/internal/ssa/dom.go:func (cache *Cache) scratchBlocksForDom(maxBlockID int) (a, b, c, d, e, f, g []ID) {
src/net/rpc/server.go:func (server *Server) readRequest(codec ServerCodec) (service *service, mtype *methodType, req *Request, argv, replyv reflect.Value, keepReading bool, err error) {
src/crypto/tls/prf.go:func keysFromMasterSecret(version uint16, suite *cipherSuite, masterSecret, clientRandom, serverRandom []byte, macLen, keyLen, ivLen int) (clientMAC, serverMAC, clientKey, serverKey, clientIV, serverIV []byte) {
src/cmd/compile/internal/ssa/gen/rulegen.go:func parseValue(val string, arch arch, loc string) (op opData, oparch string, typ string, auxint string, aux string, args []string) {
src/cmd/compile/internal/ssa/dom.go:func (cache *Cache) scratchBlocksForDom(maxBlockID int) (a, b, c, d, e, f, g []ID) {
</code></pre>
<p>Of the 4202 functions that return two values:</p>
<ul>
<li>3260 return an error as the second value</li>
<li>306 return a bool as the second value</li>
<li>175 return an int as the second value</li>
</ul>
<p>So using the go compiler and stdlib as an example, you can infer that:</p>
<ul>
<li>Returning multiple values is much less common than returning a single value</li>
<li>If returning multiple values, it's by far most common to return an error as the second value</li>
<li>The only other semi-common cases are returning a bool or int as the second value (e.g. math/big/Rat.Float64)</li>
</ul>
<p>If you're interested in some outliers from the standard library that don't fit the above:</p>
<pre><code>src/image/color/color.go:func (c RGBA) RGBA() (r, g, b, a uint32)
src/math/big/floatconv.go:func (z *Float) Parse(s string, base int) (f *Float, b int, err error)
src/image/format.go:func Decode(r io.Reader) (Image, string, error)
src/mime/mediatype.go:func ParseMediaType(v string) (mediatype string, params map[string]string, err error)
src/strconv/quote.go:func UnquoteChar(s string, quote byte) (value rune, multibyte bool, tail string, err error)
</code></pre></pre>gboxy: <pre><p>natefinch, baliance, thank you </p>
<p>i made this example for clearing</p>
<p>package main</p>
<p>import "fmt"</p>
<p>var (</p>
<pre><code>a, b, x, y int
</code></pre>
<p>)</p>
<p>func main() {</p>
<pre><code>fmt.Println("Enter numbers! ")
fmt.Scanf("%d %d", &a, &b)
x, y := returnNumbers(a, b)
fmt.Println(a, b, x, y)//5 6 25 36
</code></pre>
<p>}</p>
<p>func returnNumbers(a, b int) (int, int) {</p>
<pre><code>a = a * a
b = b * b
return a, b
</code></pre>
<p>}</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传