直接上代码看个例子吧:

package main
import "fmt" //返回 X+Y 和 X*Y
func Computer(X, Y int) (int, int) {
return X+Y, X*Y
} func main() {
x :=
y := a, b := Computer(x, y) fmt.Printf("%d + %d = %d\n", x, y, a)
fmt.Printf("%d * %d = %d\n", x, y, b)
}

上面的例子我们可以看到直接返回了两个参数,当然我们也可以命名返回参数的变量,

这个例子里面只是用了两个类型,我们也可以改成如下这样的定义,然后返回的时候不用带上变量名,因为直接在函数里面初始化了:

package main
import "fmt" //返回 X+Y 和 X*Y
func Computer(X, Y int) (add int, multiplied int) { //这里命令返回参数的变量add 和 multiplied
add = X+Y
multiplied = X*Y
return
} func main() {
x :=
y := a, b := Computer(x, y) fmt.Printf("%d + %d = %d\n", x, y, a)
fmt.Printf("%d * %d = %d\n", x, y, b)
}

但是:如果你的函数是导出的(首字母大写),官方建议:最好命名返回值,因为不命名返回值,虽然使得代码更加简洁了,但是会造成生成的文档可读性差。

Go中多个返回值的技巧的更多相关文章

  1. javascript中的函数返回值(return)

    有些情况,我们希望获取到函数的执行结果,也就是我们需要在函数以外的地方处理执行结果,而不是在函数内部处理.这时我们就需要为函数设一个返回值,也就是return,即函数执行完毕以后返回的结果. 若在函数 ...

  2. MyBatis中Mapper的返回值类型

    insert.update.delete语句的返回值类型 对数据库执行修改操作时,数据库会返回受影响的行数. 在MyBatis(使用版本3.4.6,早期版本不支持)中insert.update.del ...

  3. C++中函数的返回值

    原文 [ 函数的返回值用于初始化在调用函数处创建的临时对象.在求解表达式时,如果需要一个地方储存其运算结果,编译器会创建一个没有命名的对象,这就是 临时对象.temporary object ] -- ...

  4. loadrunner脚本中参数化和返回值输出log到外部文件

    loadrunner脚本中参数化和返回值输出log到外部文件 很多时候,我们在做性能测试之前,需要造数据,但是使用的这些参数化数据和生成的返回数据在后面的测试都会用的,所以我们需要在造数据过程中,将参 ...

  5. ResultMap和ResultType在使用中的区别、MyBatis中Mapper的返回值类型

    在使用mybatis进行数据库连接操作时对于SQL语句返回结果的处理通常有两种方式,一种就是resultType另一种就是resultMap,下面说下我对这两者的认识和理解 resultType:当使 ...

  6. jQuery中ajax如何返回值到上层函数

    jQuery中ajax如何返回值到上层函数 一.总结 一句话总结: ajax的同步操作即可,设置 async: false, 二.jquery的同步操作 var can_submit=true; $. ...

  7. 1.2Web API 2中的Action返回值

    本主题描述 ASP.NET Web API 将返回值转换从一个控制器动作到 HTTP 响应消息. 一个 Web API 控制器动作可以返回下列任一操作 ︰ 1.void 2.IHttpActionRe ...

  8. C++中函数中没写返回值会怎么样?

    先看这一段代码: /* P125 清单7.15 使用迭代求第N个Fibonacci数 */ #include <iostream> int fib(int position); int m ...

  9. ThinkPHP中数据库操作返回值总结

    转自:http://www.baiwar.com/post/thinkphp-database-operations-in-the-return-value.html Thinkphp中的Think\ ...

随机推荐

  1. css与html基础收集

    1.css去掉iPhone.iPad默认按钮样式 nput[type="button"], input[type="submit"], input[type=& ...

  2. Linux jdk 环境变量配置

    备忘,引用自:http://blog.csdn.net/lzwglory/article/details/54233248 1. 永久修改,对所有用户有效  # vi /etc/profile //按 ...

  3. 基于js的数据结构与算法-数组

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  4. Data Center手册(4):设计

    基础架构 拓扑图 Switching Path L3 routing at aggregation layer L2 switching at access layer L3 switch融合了三种功 ...

  5. Hash及HashMap简介

    Hash简介: Hash,一般翻译做“散列”,也有直接音译为“哈希”的,就是把任意长度的输入(又叫做预映射pre-image)通过散列算法变换成固定长度的输出,该输出就是散列值.这种转换是一种压缩映射 ...

  6. Redis基础入门,Redis的优点也特点,Redis五种数据类型

    Redis是一个开源,高级的键值存储和一个适用的解决方案,用于构建高性能,可扩展的Web应用程序. 1.Redis的主要特点 Redis有三个主要特点,使它优越于其它键值数据存储系统 - Redis将 ...

  7. [Swift]LeetCode9. 回文数 | Palindrome Number

    Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same back ...

  8. [Swift]LeetCode34. 在排序数组中查找元素的第一个和最后一个位置 | Find First and Last Position of Element in Sorted Array

    Given an array of integers nums sorted in ascending order, find the starting and ending position of ...

  9. [Swift]LeetCode524. 通过删除字母匹配到字典里最长单词 | Longest Word in Dictionary through Deleting

    Given a string and a string dictionary, find the longest string in the dictionary that can be formed ...

  10. [Swift]LeetCode793. 阶乘函数后K个零 | Preimage Size of Factorial Zeroes Function

    Let f(x) be the number of zeroes at the end of x!. (Recall that x! = 1 * 2 * 3 * ... * x, and by con ...