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(一) 写了删删了写,反复几次,对自己的描述很不 ...
随机推荐
- eclipse运行mapreduce的wordcount
1,eclipse安装hadoop插件 插件下载地址:链接: https://pan.baidu.com/s/1U4_6kLFNiKeLsGfO7ahXew 提取码: as9e 下载hadoop-ec ...
- jumpserver0.4.0与python3版本安装
环境: 系统:CentOS 6.5 Python版本:Python3.6 安装目录:/Data/apps/ 一. 环境准备: 1. 基本工具库: # yum -y install sqlite-de ...
- Redis-负载均衡
摘要 在nginx里面配置一个upstream,然后把相关的服务器ip都配置进去.然后采用轮询的方案,然后在nginx里面的配置项里,proxy-pass指向这个upstream,这样就能实现负载均衡 ...
- Win7 Eclipse 搭建spark java1.8编译环境,JavaRDD的helloworld例子
[学习笔记] Win7 Eclipse 搭建spark java1.8编译环境,JavaRDD的helloworld例子: 在eclipse oxygen上创建一个普通的java项目,然后把spark ...
- ARC081E. Don't Be a Subsequence
$\newcommand{\dp}{\mathsf{dp}}$ $\newcommand{\next}{\mathsf{next}}$ Let $S$ be a string of lower cas ...
- Java学习总结一 数据类型
@Java300 学习总结 一.Java 基本数据类型分类如下: 整型变量:byte.short.int.long 浮点型变量:float.double 字符型变量:char 布尔型变量:boolea ...
- 【KMP】Radio Transmission
问题 L: [KMP]Radio Transmission 题目描述 给你一个字符串,它是由某个字符串不断自我连接形成的.但是这个字符串是不确定的,现在只想知道它的最短长度是多少. 输入 第一行给出字 ...
- ACCESS打得开mdb,但打不开表,弹框提示未知错误。
我的电脑有个一个奇怪的错误ACCESS能打开mdb数据库,但是打不开表,一打开就提示错误,只有俩字“未知”.重装OFFICE也不行!而且电脑上使用Access数据库的应用软件都不行了!都提示“未知”错 ...
- 深入理解Java自动装箱拆箱机制
1.自动装箱与拆箱的定义 装箱就是自动将基本数据类型转换为包装器类型(int-->Integer): 拆箱就是自动将包装器类型转换为基本数据类型(Integer-->int). Java中 ...
- 如何爬取icourse163 中国慕课上课程信息(上),
中国大学MOOC网上有着特别完善的课程信息,我觉得这是一份可以让我们充分利用的资源 那么,接下来的问题就是我们该如何爬取这里的资源 选择其中的计算机课程进行尝试 import requests fro ...