函数的传值中,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.
    But
    iAreaId := int(val)is not any of the cases 1.-7.

地址:关于interface{}做为参数

野生翻译如下:
我懒得翻了。。。。

Golang中用interface{}接收任何参数与强转的更多相关文章

  1. 【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 ...

  2. golang的interface剖析

      背景: golang的interface是一种satisfied式的.A类只要实现了IA interface定义的方法,A就satisfied了接口IA.更抽象一层,如果某些设计上需要一些更抽象的 ...

  3. Golang的Interface是个什么鬼

    问题概述 Golang的interface,和别的语言是不同的.它不需要显式的implements,只要某个struct实现了interface里的所有函数,编译器会自动认为它实现了这个interfa ...

  4. Struts系列笔记(6)---action接收请求参数

    action接收请求参数 在web开发中,去接收请求参数来获得表单信息非常的常见,自己也总结整理了有关Struts2通过action接收请求参数的几种方法. Struts2 提供三种数据封装的方式: ...

  5. Struts框架(6)---action接收请求参数

    action接收请求参数 在web开发中,去接收请求参数来获得表单信息非常的常见,自己也总结整理了有关Struts2通过action接收请求参数的几种方法. Struts2 提供三种数据封装的方式: ...

  6. golang学习笔记8 beego参数配置 打包linux命令

    golang学习笔记8 beego参数配置 打包linux命令 参数配置 - beego: 简约 & 强大并存的 Go 应用框架https://beego.me/docs/mvc/contro ...

  7. Golang 的 `[]interface{}` 类型

    Golang 的 []interface{} 类型 我其实不太喜欢使用 Go 语言的 interface{} 类型,一般情况下我宁愿多写几个函数:XxxInt, XxxFloat, XxxString ...

  8. webapi 控制器接收POST参数时必须以对象的方式接收

    webapi    控制器接收POST参数时必须以对象的方式接收

  9. SpringMVC——接收请求参数和页面传参

    Spring接收请求参数: 1.使用HttpServletRequest获取 @RequestMapping("/login.do") public String login(Ht ...

随机推荐

  1. 《Python基础教程》第二章:列表和元组(2)

    list函数可以将字符串转换为列表 ' '.join(somelist)可以将列表转换为字符串 从列表中删除元素可以使用del语句来实现 方法是与对象有紧密联系的函数:对象.方法(参数) append ...

  2. Qt实现多国语言(即界面翻译)可实时进行切换

    .在工程文件添加 TRANSLATIONS = debug/lang_English.ts \ debug/lang_Chinese.ts .在需要翻译的地方用上tr 例如:setText(tr(&q ...

  3. pro git 读书笔记 3 Git 分支

    分支 新建分支:git branch 分支名 切换到该分支:git checkout 分支名 补充:以上两条语句等价于一句:git checkout -b 分支名 转换分支的时候最好保持一个清洁的工作 ...

  4. [React] Always useMemo your context value

    Have a similar post about Reac.memo. This blog is the take away from this post. To understand why to ...

  5. react创建项目后运行npm run eject命令将配置文件暴露出来时报错解决方法

    最近在用create-react-app创建项目,因要配置各种组件,比如babel,antd等, 需要运行npm run eject命令把项目的配置文件暴露出来,但是还是一如既然碰到报错,因为是在本地 ...

  6. 清除Tomcat缓存

        删除tomcat目录下的缓存文件分别位于:                   tomcat/work                 tomcat/temp                  ...

  7. ORA-03113:通信通道的文件结尾处理

     ORA-03113:通信通道的文件结尾执行:alter system set "_optimizer_join_elimination_enabled"=false; cmdsq ...

  8. asp.net上传超大文件解决方案

    ASP.NET上传文件用FileUpLoad就可以,但是对文件夹的操作却不能用FileUpLoad来实现. 下面这个示例便是使用ASP.NET来实现上传文件夹并对文件夹进行压缩以及解压. ASP.NE ...

  9. 'findstr' 不是内部或外部命令,也不是可运行的程序或批处理文件

    今天通过windows cmd客户端输入: solr.cmd start ,启动solr时,提示"findstr' 不是内部或外部命令,也不是可运行的程序或批处理文件" 这是PAT ...

  10. [CSP-S模拟测试]:那一天我们许下约定(DP+组合数学)

    题目传送门(内部题2) 输入格式 每个测试点有多组测试数据.对于每组数据,有一行共三个整数$N$,$D$,$M$含义如题.输入结束标识为$“0 0 0”$ (不含引号). 输出格式 对于每组数据,输出 ...