Golang中用interface{}接收任何参数与强转
函数的传值中,interface{}是可以传任意参数的,就像java的object那样。
下面上我第一次想当然写的 ** 错误 **代码
package main
func main() {
Any(2)
Any("666")
}
func Any(v interface{}) {
v1:=int(v)
println(v1)
}
我只是想它能通过编译而已,因为上面的错误代码并没有任何的语法错误,心里只有666想说,下面是编译的错误提示:
cannot convert v (type interface {}) to type int: need type assertion
正确的代码就可以保证程序不出什么差错。
package main
func main() {
Any(2)
Any("666")
}
func Any(v interface{}) {
if v2, ok := v.(string);ok{
println(v2)
}else if v3,ok2:=v.(int);ok2{
println(v3)
}
}
输出如下
2
666
不得不惊叹go的严谨啊。java写类似代码一下就编过去了。
无形之中我喷了java 好多好多,但它毕竟是20年前的语言,比我年纪还大,有些问题是真的没办法,现在基于jvm的kotlin,如果我当时没有放弃android的话,我想我现在的主要工作语言就是它了。
解释一下,为什么Golang有些情况下,强转可以,有点情况下,强转不可以:
其实stack overflow一个国外大神说的很好了,
原话+野生翻译
The reason why you cannot convert an interface typed value are these rules in the referenced specs parts:
Conversions are expressions of the form T(x) where T is a type and x is an expression that can be converted to type T.
A non-constant value x can be converted to type T in any of these cases:
- x is assignable to T.
- x's type and T have identical underlying types.
- x's type and T are unnamed pointer types and their pointer base types have identical underlying types.
- x's type and T are both integer or floating point types.
- x's type and T are both complex types.
- x is an integer or a slice of bytes or runes and T is a string type.
- x is a string and T is a slice of bytes or runes.
ButiAreaId := int(val)
is not any of the cases 1.-7.
野生翻译如下:
我懒得翻了。。。。
Golang中用interface{}接收任何参数与强转的更多相关文章
- 【spring mvc】后台spring mvc接收List参数报错如下:org.springframework.beans.BeanInstantiationException: Failed to instantiate [java.util.List]: Specified class is an interface
后台spring mvc接收List参数报错如下:org.springframework.beans.BeanInstantiationException: Failed to instantiate ...
- golang的interface剖析
背景: golang的interface是一种satisfied式的.A类只要实现了IA interface定义的方法,A就satisfied了接口IA.更抽象一层,如果某些设计上需要一些更抽象的 ...
- Golang的Interface是个什么鬼
问题概述 Golang的interface,和别的语言是不同的.它不需要显式的implements,只要某个struct实现了interface里的所有函数,编译器会自动认为它实现了这个interfa ...
- Struts系列笔记(6)---action接收请求参数
action接收请求参数 在web开发中,去接收请求参数来获得表单信息非常的常见,自己也总结整理了有关Struts2通过action接收请求参数的几种方法. Struts2 提供三种数据封装的方式: ...
- Struts框架(6)---action接收请求参数
action接收请求参数 在web开发中,去接收请求参数来获得表单信息非常的常见,自己也总结整理了有关Struts2通过action接收请求参数的几种方法. Struts2 提供三种数据封装的方式: ...
- golang学习笔记8 beego参数配置 打包linux命令
golang学习笔记8 beego参数配置 打包linux命令 参数配置 - beego: 简约 & 强大并存的 Go 应用框架https://beego.me/docs/mvc/contro ...
- Golang 的 `[]interface{}` 类型
Golang 的 []interface{} 类型 我其实不太喜欢使用 Go 语言的 interface{} 类型,一般情况下我宁愿多写几个函数:XxxInt, XxxFloat, XxxString ...
- webapi 控制器接收POST参数时必须以对象的方式接收
webapi 控制器接收POST参数时必须以对象的方式接收
- SpringMVC——接收请求参数和页面传参
Spring接收请求参数: 1.使用HttpServletRequest获取 @RequestMapping("/login.do") public String login(Ht ...
随机推荐
- 201871010101-陈来弟《面向对象程序设计(java)》第十七周学习总结
实验十七 线程同步控制 实验时间 2018-12-10 第一部分:理论知识 1.多线程并发执行中的问题 ◆多个线程相对执行的顺序是不确定的. ◆线程执行顺序的不确定性会产生执行结果的不确定性. ◆在 ...
- PLS做soft particle
这个pixel local storage frame fetch 可以一个pass做出soft particle/deferred lighting/soft edge water programb ...
- python3安装模块,摘自网上
配置好Python3.6和pip3安装EPEL和IUS软件源 yum install epel-release -y yum install https://centos7.iuscommunity. ...
- 卸载nginx
sudo apt-get remove nginx nginx-common # 卸载删除除了配置文件以外的所有文件. sudo apt-get purge nginx nginx-common # ...
- [学习笔记] 平衡树——Treap
前置技能:平衡树前传:BST 终于学到我们喜闻乐见的平衡树啦! 所以我们这次讲的是平衡树中比较好写的\(Treap\). (以后会写splay的先埋个坑在这) 好了,进入正题. step 1 我们知道 ...
- webuploader如何判断是否上传的是空文件?
在'beforeFileQueued'事件中可以判断: // 当有文件被添加进队列的时候 uploader.on( 'beforeFileQueued', function( file ) { if( ...
- vue路由登录拦截(vue router登录权限控制)
实现原理: 哪些路由需要验证需要在路由文件router/index.js中指定: { path: '/', component: Home, name: 'Home', iconCls: 'fa fa ...
- Python之hashlib模块的使用
hashlib模块主要的作用: 加密保护消息安全,常用的加密算法如MD5,SHA1等. 1.查看可用的算法有哪些 #!/usr/bin/env python # -*- coding: utf-8 - ...
- java+大文件上传解决方案
众所皆知,web上传大文件,一直是一个痛.上传文件大小限制,页面响应时间超时.这些都是web开发所必须直面的. 本文给出的解决方案是:前端实现数据流分片长传,后面接收完毕后合并文件的思路. 实现文件夹 ...
- 分析 JUnit 框架源代码
本文转载至http://www.ibm.com/developerworks/cn/java/j-lo-junit-src/ 分析 JUnit 框架源代码 理解 JUnit 测试框架实现原理和设计模式 ...