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

Type assertions

For an expression x of interface type and a type T, the primary expression

x.(T)

asserts that x is not nil and that the value stored in x is of type T. The notation x.(T) is called a type assertion.


Go has conversions.

The Go Programming Language Specification

Conversions

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.

answered Jul 4 '14 at 19:45
peterSO

101k16172186

golang type conversion的更多相关文章

  1. JavaScript Type Conversion

    Data Types 5 Data Types string, number, boolean, object, function 3 Object Types object, array, date ...

  2. error: expected constructor, destructor, or type conversion before '.' token

    今天写代码是遇到这样一个问题error: expected constructor, destructor, or type conversion before '.' token:立马网上查,原来是 ...

  3. 【错误】expected constructor, destructor, or type conversion before '.' token - 第八个游侠的日志 - 网易博客

    [错误]expected constructor, destructor, or type conversion before '.' token - 第八个游侠的日志 - 网易博客 [错误]expe ...

  4. Type Conversion

    文章著作权归作者所有.转载请联系作者,并在文中注明出处,给出原文链接. 本文原更新于作者的github博客,这里给出链接. 什么是转换 转换是接受一个类型的值并使用它作为另一个类型的等价值的过程.转换 ...

  5. Spring Framework 官方文档学习(四)之Validation、Data Binding、Type Conversion(一)

    题外话:本篇是对之前那篇的重排版.并拆分成两篇,免得没了看的兴趣. 前言 在Spring Framework官方文档中,这三者是放到一起讲的,但没有解释为什么放到一起.大概是默认了读者都是有相关经验的 ...

  6. Spring Framework 官方文档学习(四)之Validation、Data Binding、Type Conversion(二)

    接前一篇 Spring Framework 官方文档学习(四)之Validation.Data Binding.Type Conversion(一) 本篇主要内容:Spring Type Conver ...

  7. delphi 10.1 berlin datasnap提交clientdataset.delta报:invalid variant type conversion(类型转换错误)问题的解决

    delphi 10.1 berlin datasnap提交clientdataset.delta报:invalid variant type conversion(类型转换错误)问题的解决,需要打这个 ...

  8. 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 ...

  9. Spring Framework 官方文档学习(四)之Validation、Data Binding、Type Conversion

    本篇太乱,请移步: Spring Framework 官方文档学习(四)之Validation.Data Binding.Type Conversion(一) 写了删删了写,反复几次,对自己的描述很不 ...

随机推荐

  1. 【VS开发】QueryPerformanceFrequency与QueryPerformanceCounter的使用

    LARGE_INTEGER tima,timb; QueryPerformanceCounter(&tima); 在 Windows Server 2003 和 WindowsXP 中使用 Q ...

  2. OpenCV.概念(读书笔记)

    ZC:学习OpenCV.pdf 1.多通道矩阵(学习OpenCV.pdf) 1.1.在学习opencv的时候看到多通道矩阵这一概率,恳求大神告诉我一下什么意思_百度知道.html(https://zh ...

  3. vue组件库element-ui 的Table内容显示不更新

    一.问题原因: 因为数组直接赋值不能被 Object.defineProperty 检测到.  二.解决方法 所以应该要使用this.$set(‘对象名’,要修改的属性名,属性值),这样新添加的属性值 ...

  4. python初学第一节课

    ### python的基础 推荐学习python的一些资料, 首先就是书籍<Python从入门到实践>, 这本书评分很高, 主要是对Python讲解的比较清楚, 后面还有具体的实战项目 推 ...

  5. 对于大于等于3的整数n,在区间【n,3/2 * n】中一定存在一个素数

    对于大于3的整数n,在区间[n,3/2 * n]中一定存在一个素数

  6. 日常工作问题解决:记一次centos7上的lvm表错误解决过程

    问题描述: 公司大数据hadoop2服务器采用电信云服务器,后来故障,电信恢复该服务器,需要重新部署程序,需要扩展lvm分区,但是使用pvsan命令发现有报错信息,需要解决以防重启后,因挂载问题,无法 ...

  7. UWP 保存音乐或视频缩略图图片到本地

    开发项目时,有时需要将本地媒体文件的缩略图保存到本地,下面是源码. 需要打开Package.appxmanifest 功能 图片库 访问权限. <Page x:Class="SaveB ...

  8. 2.5路由网关:Zuul

    在原有的工程上,创建一个新的工程 创建service-zuul工程 其pom.xml文件如下: <?xml version="1.0" encoding="UTF- ...

  9. Linux挂载磁盘&kuoron

    1.添加磁盘 物理服务器直接插上硬盘即可,虚拟机的话给直接添加磁盘即可,不懂的可以自行百度,比较简单. 2.管理磁盘分区,fdisk命令. 在Linux系统中,管理硬盘设备最常用的方法就当属 fdis ...

  10. jsp页面报错,各种错误码意思

    基本原则: 2xx = Success(成功) 3xx = Redirect(重定向) 4xx = User error(客户端错误) 5xx = Server error(服务器端错误) 状态码 ( ...