go 语言开发中,经常会在函数中碰到使用 insterface{} 作为接收任意参数,但是我们接收的数据经常是需要做类型转换,由于是初学者,因此,初次转换我是直接就

func New(parameters map[string]interface{}) (*driver, error){
hostname, _ := string(parameters["HostName"])
fmt.Println(parameters)
machines := []string{hostname}
client := etcd.NewClient(machines)
return &driver{
etcd: client,
}, nil
}

可以看到,我直接使用了 hostname, _ := string(parameters["HostName"]) 进行转换,不出意外,发生了一些不愉快的错误

[vagrant@localhost etcd]$ godep go test
# configcenter/storage/driver/etcd
./etcd.go::: cannot assign values to variables
./etcd.go::: cannot convert parameters["HostName"] (type interface {}) to type string: need type assertion
FAIL configcenter/storage/driver/etcd [build failed]
godep: go exit status

提示类型无法进行转换,于是陷入了深深的沉思中——看到了关键语句

 need type assertion

顺利的找到了解决方式:

# 替换强制转换语句
hostname := string(parameters["HostName"])
# 如下,为正确表达式
hostname := parameters["HostName"].(string)
hostname, ok := parameters["HostName"].(string)

但是,依然没有想出来是什么原因。于是上了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:

  1. x is assignable to T.
  2. x's type and T have identical underlying types.
  3. x's type and T are unnamed pointer types and their pointer base types have identical underlying types.
  4. x's type and T are both integer or floating point types.
  5. x's type and T are both complex types.
  6. x is an integer or a slice of bytes or runes and T is a string type.
  7. x is a string and T is a slice of bytes or runes.

船新智能翻译,给你不一样的体验:

  1. x被赋值为t。
  2. x的类型和t具有相同的基础类型。
  3. x的类型和t是未命名的指针类型,它们的指针基类型具有相同的基础类型。
  4. x的类型和t都是整数或浮点类型。
  5. x的类型和t都是复杂类型。
  6. x是一个整数或一片字节或符文,且T是一个字符串类型。
  7. X是一个字符串,t是一片字节或字符。

但是,

  hostname := string(parameters["HostName"])

不属于上述七个情景中的一个。



(type interface {}) to type string的更多相关文章

  1. Golang报错:Cannot convert expression of type interface{} to type []byte

    在使用golang实现后端登录逻辑的时候,碰到下面的问题:Cannot convert expression of type interface{} to type []byte 首先介绍下问题出现的 ...

  2. Go语言中cannot convert adminname (type interface {}) to type *: need type assertion的解决办法

    解决的办法是把string(adminname)替换为adminname.(string).其它类型也是类似.

  3. 【区分】Typescript 中 interface 和 type

    在接触 ts 相关代码的过程中,总能看到 interface 和 type 的身影.只记得,曾经遇到 type 时不懂查阅过,记得他们很像,相同的功能用哪一个都可以实现.但最近总看到他们,就想深入的了 ...

  4. Exception in thread "main" org.apache.ibatis.binding.BindingException: Type interface com.test.bean.groupMapper is not known to the MapperRegistry.

    Exception in thread "main" org.apache.ibatis.binding.BindingException: Type interface com. ...

  5. 解决 'Could not convert variant of type (NULL) into type (String)

    写存储过程中有不允许为空的字段,在客户端转化取数时显示 Could not convert variant of type (NULL) into type (String) 可以在存储过程中使用is ...

  6. ssm之mapper异常 Result Maps collection already contains value for com.ssj.mapper.UserMapper 和 Type interface com.ssj.mapper.UserMapper is already known to the MapperRegistry.

    异常一:Result Maps collection already contains value for com.ssj.mapper.XXXMapper 原因分析:XXXmapper.xml文件中 ...

  7. 报错org.apache.ibatis.binding.BindingException: Type interface com.atguigu.mybatis.bean.dao.EmployeeMapper is not known to the MapperRegistry.

    报错org.apache.ibatis.binding.BindingException: Type interface com.atguigu.mybatis.bean.dao.EmployeeMa ...

  8. Eclipse 出现select type (? = any character,*= any String,Tz=TimeZone)

    在eclipse中想运行project的时候,往往是右键项目名称---->run As --->Java Application 但是会弹出窗口显示select type (? = any ...

  9. MyBatis—— org.apache.ibatis.binding.BindingException: Type interface com.web.mybatis.i.PersonMapper is not known to the MapperRegistry.

    报错信息: Exception in thread "main" org.apache.ibatis.binding.BindingException: Type interfac ...

随机推荐

  1. UVa-227-谜题

    这题的话,我们读入的时候,可以用scanf单个读入字符,也可以用getchar函数来读入. scanf scanf读入串字符的时候,遇到空格.回车和TAB等空白字符就会停止读入,但是如果读入单个字符就 ...

  2. MySQL Group Replication(组复制MGR)

    MGR基本要求: 1.InnoDB存储引擎 2.主键,每个表必须具有已定义的主键或等效的主键,其中等效项是非null唯一键 3.IPv4网络 4.网络性能 5.开启二进制日志并开启GTID模式 6.m ...

  3. GIMP选择区域Selection Editor

    如图我要选择该图的衣服部分和这个球的部分, 选择Select下的Selection Editor工具,然后点击魔法棒工具(Fuzzy Select Tool),选择衣服: 需要注意以下白色部分是选择的 ...

  4. 详解css媒体查询

    简介 媒体查询(Media Queries)早在在css2时代就存在,经过css3的洗礼后变得更加强大bootstrap的响应式特性就是从此而来的. 简单的来讲媒体查询是一种用于修饰css何时起作用的 ...

  5. perl:_DATA_ _LINE_ _FILE_

    这三个应该是句柄: _DATA_ _FILE_ _LINE_ 没有找到具体介绍...记录于此,已被后续补充.

  6. 基于顺序链表的栈的顺序存储的C风格实现

    头文件: #ifndef _SEQSTACK_H_ #define _SEQSTACK_H_ typedef void SeqStack; //创建一个栈 SeqStack* SeqStack_Cre ...

  7. cs231n课程索引

    课程资源 课程官网 课程视频-youtube 课程视频-字幕版 官方笔记 官方笔记-中文版 课程作业参考答案

  8. centos 部署 自定义(succes)

    安装前先检查一下有没有安装好了的JDK,Tomcat,MySQL,不过一般都没有. 1.安装JDK 1.1 下载jdk,可以到官网查看不同版本的下载地址 wget --no-check-certifi ...

  9. Visual studio 新建网站出现序号(x)

    参考链接: http://www.zhongdaiqi.com/vs2012-new-website-name-bug/ 现象: 分析: VS新建网站出现(1) 这个问题很神秘,把网站删除掉,再创建, ...

  10. luogu2568 GCD

    先筛法求出 \([1,n]\) 间的素数,然后枚举每个素数.可以发现,对于每个素数 \(x\),它的贡献是 \([1,\lfloor n/x \rfloor]\) 间的有序互质对数. 我们钦定 \(( ...