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
x
of interface type and a typeT
, the primary expressionx.(T)
asserts that
x
is notnil
and that the value stored inx
is 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)
whereT
is a type andx
is 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(一) 写了删删了写,反复几次,对自己的描述很不 ...
随机推荐
- 安装flask_mysqldb出现报错问题解析
安装 mysqldb (pip3 install Flask-MySQLdb)报错 EnvironmentError: mysql_config not found ERROR: Command er ...
- 《Java语言程序设计》异常处理
一.请阅读并运行AboutException.java示例,然后通过后面的几页PPT了解Java中实现异常处理的基础知识. 1. 源代码为: import javax.swing.*; class A ...
- vscode插件Project Manager
参考链接:https://www.jianshu.com/p/b11532b91414
- windows10下无U盘安装ubuntu18 使用EasyUEFI(一点点体会)
一.看BIOS 先看看自己电脑的是哪种启动模式 win+R 输入 msinfo32 查看自己电脑是哪种 (UEFI还是Legacy BIOS启动模式) 查看完之后 如果是UEFI的话 go on ...
- 使用python django快速搭建微信公众号后台
前言 使用python语言,django web框架,以及wechatpy,快速完成微信公众号后台服务的简易搭建,做记录于此. wechatpy是一个python的微信公众平台sdk,封装了被动消息和 ...
- MyBatis 示例-插件
简介 利用 MyBatis Plugin 插件技术实现分页功能. 分页插件实现思路如下: 业务代码在 ThreadLocal 中保存分页信息: MyBatis Interceptor 拦截查询请求,获 ...
- django+uwsgi+nginx: websock 报502/400
耽搁了近2个月,终于解决了,主要是nginx/uwsgi/django相关的配置: 一.django工程settings.py,添加 WEBSOCKET_FACTORY_CLASS = "d ...
- mysql 5.7.19安装
从mysql官网下载的mysql5.7.19免安装版, 安装时出现问题,mysql总是启动不起来,在网上查了下资料,做个记录: .将mysql解压到指定的文件夹 .以管理员身份运行cmd .定位到my ...
- git 操作实践
git操作: - git是一个用于帮助用户实现版本控制的软件 #首先创建项目 1. cd到项目文件目录 2. 鼠标右键点击 Git Bash Here 3. git init #在项目文件目录生成 . ...
- VisualSVN Server 安装
1.去官网下载:https://www.visualsvn.com/server/download/ 2.安装: 修改为后: 3.开机自动启动.