Go used as value问题
练习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包或者给返回值进行输出。
随机推荐
- (转载)intellj idea 如何设置类头注释和方法注释
原文地址:http://www.cnblogs.com/wvqusrtg/p/5459327.html intellj idea的强大之处就不多说了,相信每个用过它的人都会体会到, ...
- Python Scrapy爬虫速成指南
序 本文主要内容:以最短的时间写一个最简单的爬虫,可以抓取论坛的帖子标题和帖子内容. 本文受众:没写过爬虫的萌新. 入门 0.准备工作 需要准备的东西: Python.scrapy.一个IDE或者随便 ...
- 使用msf对tomcat测试
1.1 使用nmap命令对目标主机进行扫描.单击桌面空白处,右键菜单选择"在终端中打开". 1.2 在终端中输入命令"nmap –sV 192.168.1.3" ...
- PHP----------安装包lnmp1.3-full安装的lnmp环境,如何安装PHP扩展
1. 如果已经安装LNMP套件,请按以下步骤处理 a. 跳转到fileinfo源代码目录` cd /root/downloads/lnmp1.2-full/src/php-7.0.7/ext/file ...
- Vue系列之 => ref获取DOM元素和组件
可以获取DOM元素,和组件中的数据,方法 <!DOCTYPE html> <html lang="en"> <head> <meta ch ...
- java并发之线程间通信
1.volatile 关键字 java 支持多个线程同时访问一个对象或对象的成员变量,而每个线程拥有这个变量的拷贝,虽然对象或成员变量分配的内存在共享内存,但每个执行的线程可以拥有一份拷贝,可以提高程 ...
- Docket 使用命令
Docket 使用命令 查 # 查询当前可以下载的镜像 docker search httpd |_ NAME:镜像仓库源的名称 |_ DESCRIPTION:镜像的描述 |_ OFFICIAL:是 ...
- 用python完成带有进度条的圆周率计算
代码如下:import math import time scale= s,m,=, print("执行开始".center(scale//2, "-")) s ...
- Spring 学习——Spring注解——Autowiring(自动装配)
装配方式 方式一:默认 方式二:byName:根据属性名称自动装配.会查找Bean容器内部所有初始化的与属性名成相同的Bean,自动装配.(需要通过set方法注入,注入Bean的id名称需要和实体类的 ...
- Spring错误——Spring AOP——org.aspectj.weaver.reflect.ReflectionWorld$ReflectionWorldException
背景:学习切面,测试前置通知功能,xml配置如下 <?xml version="1.0" encoding="UTF-8"?> <beans ...