golang type conversion
map[string]interface{} is not the same as map[string]string. Type interface{} is not the same as type string.
If they are both map[string]string:
package main
import "fmt"
func main() {
v := map[string]string{"hello": "world"}
checkCast(v)
}
func checkCast(v interface{}) {
_, isCorrectType := v.(map[string]string)
if !isCorrectType {
fmt.Printf("incorrect type")
return
}
}
Output:
[no output]
The statement v.(map[string]string) is a type assertion, not a cast.
The Go Programming Language Specification
For an expression
xof interface type and a typeT, the primary expressionx.(T)asserts that
xis notniland that the value stored inxis of typeT. The notationx.(T)is called a type assertion.
Go has conversions.
The Go Programming Language Specification
Conversions are expressions of the form
T(x)whereTis a type andxis an expression that can be converted to typeT.
- is there a way to cast in Go? – Karan Jul 4 '14 at 19:56
- 1@Karan: See my revised answer. Go has conversions. – peterSO Jul 4 '14 at 20:05
golang type conversion的更多相关文章
- JavaScript Type Conversion
Data Types 5 Data Types string, number, boolean, object, function 3 Object Types object, array, date ...
- error: expected constructor, destructor, or type conversion before '.' token
今天写代码是遇到这样一个问题error: expected constructor, destructor, or type conversion before '.' token:立马网上查,原来是 ...
- 【错误】expected constructor, destructor, or type conversion before '.' token - 第八个游侠的日志 - 网易博客
[错误]expected constructor, destructor, or type conversion before '.' token - 第八个游侠的日志 - 网易博客 [错误]expe ...
- Type Conversion
文章著作权归作者所有.转载请联系作者,并在文中注明出处,给出原文链接. 本文原更新于作者的github博客,这里给出链接. 什么是转换 转换是接受一个类型的值并使用它作为另一个类型的等价值的过程.转换 ...
- Spring Framework 官方文档学习(四)之Validation、Data Binding、Type Conversion(一)
题外话:本篇是对之前那篇的重排版.并拆分成两篇,免得没了看的兴趣. 前言 在Spring Framework官方文档中,这三者是放到一起讲的,但没有解释为什么放到一起.大概是默认了读者都是有相关经验的 ...
- Spring Framework 官方文档学习(四)之Validation、Data Binding、Type Conversion(二)
接前一篇 Spring Framework 官方文档学习(四)之Validation.Data Binding.Type Conversion(一) 本篇主要内容:Spring Type Conver ...
- delphi 10.1 berlin datasnap提交clientdataset.delta报:invalid variant type conversion(类型转换错误)问题的解决
delphi 10.1 berlin datasnap提交clientdataset.delta报:invalid variant type conversion(类型转换错误)问题的解决,需要打这个 ...
- java 反射 报错:Attempt to get java.lang.Integer field "..." with illegal data type conversion to int
类: Integer id; 反射时: Field f = User.class.getDeclaredField("id"); f.setAccessible(true); in ...
- Spring Framework 官方文档学习(四)之Validation、Data Binding、Type Conversion
本篇太乱,请移步: Spring Framework 官方文档学习(四)之Validation.Data Binding.Type Conversion(一) 写了删删了写,反复几次,对自己的描述很不 ...
随机推荐
- 《剑指offer》数组专题 (牛客10.22)
目录 // Q01 二维部分有序数组查找 [善用性质] // Q06 旋转数组中的最小元素 [二分 || 暴力] Q13 调整数组顺序使奇数位于偶数前 / Q19 顺时针打印矩阵 [使用边界变量] / ...
- 【Qt开发】【ARM-Linux开发】 QT在ARM上显示字体的问题
在PC机上利用QT开发的应用程序在设置字体时,在PC上运行,可根据自己的设置,字体随之变大或变小.而移植到ARM上运行时发现,显示字体与所设置的字体不用,字体普遍偏小.经过上网搜索发现,是环境变量字库 ...
- 【VS开发】内存映射文件3
内存映射文件 内存映射文件,是由一个文件到一块内存的映射.Win32提供了允许应用程序把文件映射到一个进程的函数 (CreateFileMapping).内存映射文件与虚拟内存有些类似,通过内存映射文 ...
- 后端根据查询条件生成excel文件返回给前端,vue进行下载
一.HTML代码 <el-col :xs="2" :md="2" :sm="3"> <el-button type=&qu ...
- 带你手写基于 Spring 的可插拔式 RPC 框架(五)注册中心
注册中心代码使用 zookeeper 实现,我们通过图片来看看我们注册中心的架构. 首先说明, zookeeper 的实现思路和代码是参考架构探险这本书上的,另外在 github 和我前面配置文件中的 ...
- noi openjudge7627:鸡蛋的硬度
http://noi.openjudge.cn/ch0206/7627/ 描述 最近XX公司举办了一个奇怪的比赛:鸡蛋硬度之王争霸赛.参赛者是来自世界各地的母鸡,比赛的内容是看谁下的蛋最硬,更奇怪的是 ...
- 学习笔记:CentOS7学习之十七: Linux计划任务与日志的管理
目录 学习笔记:CentOS7学习之十七: Linux计划任务与日志的管理 17.1 计划任务-at-cron-计划任务使用方法 17.1.1 at计划任务的使用 17.1.2 查看和删除at将要执行 ...
- IDEA插件之FindBugs
1.是个啥? Findbugs,它是一个静态分析工具,用来查找Java代码中的程序错误.它使用静态分析来识别Java程序中上百种不同类型的潜在错误. 2.安装 File -> Settings ...
- 知乎Python后端面试总结
一面 写个快速排序热热身,分析一下复杂度,如果不使用额外的空间,应该怎么写? 说一下Flask中g是怎么实现的,原理是什么? 说一下浏览器从输入url到页面渲染的过程,越详细越好: 了解web安全吗? ...
- Kubernetes---Pod笔记
⒈pod的理解 将多个容器镜像融合在一起,共享网络命名空间及容器卷 ⒉pod的分类 自助式podv 不是被控制器管理的pod,它一旦死亡不会被人给拉起来. 控制器管理的pod ...