练习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. 细说flask数据库迁移

    什么情况下要用数据库迁移? 在开发过程中,需要修改数据库模型,而且还要在修改之后更新数据库.最直接的方式就是删除旧表,但这样会丢失数据. 更好的解决办法是使用数据库迁移框架,它可以追踪数据库模式的变化 ...

  2. SQL 增加列、修改列、删除列

    SQL语句增加列.修改列.删除列 1.增加列: alter table tableName add columnName varchar(30) 2.1. 修改列类型: alter table tab ...

  3. 自动生成简单四则运算的C语言程序

    该程序是在博客园里面找的,具体是谁的找了半天没找到,无法提供它原本的链接.由于自己写的过于简单,且有一些功能暂时无法实现,所以就找了一个来应付作业,望原谅.在这个程序的源码中我改了一个错误的地方,源码 ...

  4. 盒子布局、标签特性display、浮动、定位position

    盒子模型布局: 盒子模型:每个标签都是一个盒子 盒子在页面显示在大小是:自身宽度+边框+边距(内边框+外边距) 如果一个盒子设置了边框,则边框需要被加两遍.若果设置了边距则内外边距根据设置情况要被加两 ...

  5. flutter 常用插件

    audio_recorder: any #录音.播放 flutter_sound: ^1.1.5#录音 dropdown_menu: ^1.1.0#下拉菜单 simple_permissions:#权 ...

  6. Html fieldset、legend 标签

    Html fieldset.legend 标签 <html> <body> <!-- fieldset 添加圈起标题框标签 --> <fieldset> ...

  7. Linux 管理服务启动工具

    工具:ntsysv(图形,可以关闭开启服务) 安装包:ntsysv-1.3.30.2-2.el5 工具:chkconfig –list(文字,开启关闭服务) 自定义加服务:执行脚本放入:/etc/in ...

  8. Python爬虫(四)——开封市58同城数据模型训练与检测

    前文参考: Python爬虫(一)——开封市58同城租房信息 Python爬虫(二)——对开封市58同城出租房数据进行分析 Python爬虫(三)——对豆瓣图书各模块评论数与评分图形化分析 数据的构建 ...

  9. D5 LCA 最近公共祖先

    第一题: POJ 1330 Nearest Common Ancestors POJ 1330 这个题可不是以1为根节点,不看题就会一直wa呀: 加一个找根节点的措施: #include<alg ...

  10. sqlserver 判断字符串是否是数字

    https://www.cnblogs.com/zjfblog/p/5625202.html sql2005有个函数ISNUMERIC(expression)函数:当expression为数字时,返回 ...