练习Go变参时遇到一个报错:used as value 代码如下:

// 错误代码
func myfunc(arg ...int) {
for _, n := range arg {
fmt.Printf("And the number is: %d\n", n)
}
}
func main() {
fmt.Printf(myfunc(1, 2, 3, 4, 5))
} // 报错 myfunc(1, 2, 3, 4, 5) used as value // 正确代码
func myfunc(arg ...int) {
for _, n := range arg {
fmt.Printf("And the number is: %d\n", n)
}
}
func main() {
myfunc(1, 2, 3, 4, 5)
}
// 结果:
//And the number is: 1
//And the number is: 2
//And the number is: 3
//And the number is: 4
//And the number is: 5 // 或者 正确代码
func myfunc(arg ...int) int {
m := 0
for _, n := range arg {
m += n
}
return m
}
func main() {
fmt.Printf("m = %d", myfunc(1, 2, 3, 4, 5))
}
// 结果:m = 15

  从上面代码可以看出myfunc()函数是没有返回值的,直接调用就可以,不需要使用fmt包或者给返回值进行输出。

随机推荐

  1. Java web开发环境搭配

    1.安装并配置JDK环境(1)安装过程省略(建议安装在自己指定的统一目录下,方便后期查找). (2)配置环境变量 JAVA_HOME:  C:\Java\jdk\jdk1.7.0_45 (jdk安装目 ...

  2. php实现微信网页授权回调代理

    一个简单的php文件,实现微信网页授权回调域名的代理转发  <?php function is_HTTPS() { if (!isset($_SERVER['HTTPS'])) return F ...

  3. 隐藏非选中的checkBox

    //隐藏非选中的checkBox function onlyCheckBox(){ $("#dtlTable tr:gt(0)").each(function(i) { var c ...

  4. webform ajax 异步请求

    第一种就是对应方法的请求 虽然对应方法 但还是会刷新页面 webform是基于事件的 每次请求都会出发pageload <script> $(function () { $("# ...

  5. 获取从库Seconds_Behind_Master监控主从同步

    #!/bin/bash now_date=`date "+%Y-%m-%d,%H:%M:%S"` flag_old=`cat /home/oracle/scripts/flag.t ...

  6. 【2.0】使用默认日志slf4j配置

    一.SpringBoot日志简介 SpringBoot使用Commons Logging进行所有内部日志记录,但底层日志实现保持开放状态.为Java Util Logging,Log4j2和Logba ...

  7. 论文笔记:Improving Deep Visual Representation for Person Re-identification by Global and Local Image-language Association

    Improving Deep Visual Representation for Person Re-identification by Global and Local Image-language ...

  8. 如何恢复IIS出厂默认设置

    How to restore IIS settings and Default Web Site? http://superuser.com/questions/704850/how-to-resto ...

  9. burpsuit 无法导入证书,抓取https的解决办法

    想用burpsuit中转https流量,需要安装证书: 确保浏览器能访问http 后,访问:http://burp/ 点击右上角下载证书. 然后导入,这些网上都有方法. 但如果你试了后: ①提示导入失 ...

  10. Maven 的 settings.xml 配置中的mirror节点

    maven2的setting.xml大家都知道,里面有个mirrors节点,用来配置镜像URL. mirrors可以配置多个mirror,每个mirror有id,name,url,mirrorOf属性 ...