函数的传值中,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. 微服务框架SpringCloud与Dubbo

    #v1.0.0# 1.背景 Dubbo,是阿里巴巴服务化治理的核心框架,并被广泛应用于阿里巴巴集团的各成员站点.阿里巴巴近几年对开源社区的贡献不论在国内还是国外都是引人注目的,比如:JStorm捐赠给 ...

  2. request_time和upstream_response_time详解

    下图是request_time. 下图是upstream_response_time. 精准的描述就是:request_time是从接收到客户端的第一个字节开始,到把所有的响应数据都发送完为止.ups ...

  3. bat批处理文件

    将某个文件夹中的所有txt文件合并到a.txt中,如果文件比较多的话,手动会很费时,编写程序也很麻烦,这个时候就可以用批处理文件,如下: type *.txt > a.txt 把上面这行粘贴到新 ...

  4. select * from a,b探讨

    select * from a,b探讨 今天看同事代码里使用了select * from a,b where a.id=b.id,而我平时都是使用select * from a inner join ...

  5. Windows10启用或关闭Windows功能一直显示请稍候

    1.运行service.msc打开服务列表,找到Windows Modules Installer服务进行重启.如果重启失败,可以重启电脑后再次启动该服务. 2.此时运行controller打开控制面 ...

  6. poj 3069 贪心+区间问题

    Saruman's Army Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5989   Accepted: 3056 De ...

  7. vfprintf()函数

    函数声明:int vfprintf(FILE *stream, const char *format, va_list arg) 函数参数: stream—这是指向了FILE对象的指针,该FILE对象 ...

  8. React 的 DOM 添加多个点击事件

    第一直觉代码如下:后果是写在后面的事件函数覆盖前面的事件函数,只执行第二条(弹出 222). import React, { Component, Fragment } from 'react' ex ...

  9. Ansible常用模块之命令类模块

    Command模块 在远程节点上执行命令 [root@tiandong ~]# ansible all -m command -a "ls" 在远程主机上执行ls命令. [root ...

  10. 初识 Premiere

    本记录基于Premiere Pro CC 2015.3,编号不连贯,以视频编号为准,对应视频没有有用信息的没有记录. 1.3 基本工作界面和预设工作区 将面板独立出来:按住Ctrl拖动窗口 将关闭的面 ...