(type interface {}) to type string
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:
- x is assignable to T.
- x's type and T have identical underlying types.
- x's type and T are unnamed pointer types and their pointer base types have identical underlying types.
- x's type and T are both integer or floating point types.
- x's type and T are both complex types.
- x is an integer or a slice of bytes or runes and T is a string type.
- x is a string and T is a slice of bytes or runes.
船新智能翻译,给你不一样的体验:
- x被赋值为t。
- x的类型和t具有相同的基础类型。
- x的类型和t是未命名的指针类型,它们的指针基类型具有相同的基础类型。
- x的类型和t都是整数或浮点类型。
- x的类型和t都是复杂类型。
- x是一个整数或一片字节或符文,且T是一个字符串类型。
- X是一个字符串,t是一片字节或字符。
但是,
hostname := string(parameters["HostName"])
不属于上述七个情景中的一个。
(type interface {}) to type string的更多相关文章
- Golang报错:Cannot convert expression of type interface{} to type []byte
在使用golang实现后端登录逻辑的时候,碰到下面的问题:Cannot convert expression of type interface{} to type []byte 首先介绍下问题出现的 ...
- Go语言中cannot convert adminname (type interface {}) to type *: need type assertion的解决办法
解决的办法是把string(adminname)替换为adminname.(string).其它类型也是类似.
- 【区分】Typescript 中 interface 和 type
在接触 ts 相关代码的过程中,总能看到 interface 和 type 的身影.只记得,曾经遇到 type 时不懂查阅过,记得他们很像,相同的功能用哪一个都可以实现.但最近总看到他们,就想深入的了 ...
- 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. ...
- 解决 'Could not convert variant of type (NULL) into type (String)
写存储过程中有不允许为空的字段,在客户端转化取数时显示 Could not convert variant of type (NULL) into type (String) 可以在存储过程中使用is ...
- 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文件中 ...
- 报错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 ...
- Eclipse 出现select type (? = any character,*= any String,Tz=TimeZone)
在eclipse中想运行project的时候,往往是右键项目名称---->run As --->Java Application 但是会弹出窗口显示select type (? = any ...
- 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 ...
随机推荐
- vuex其实超简单,只需3步
前言 之前几个项目中,都多多少少碰到一些组件之间需要通信的地方,而因为种种原因,event bus 的成本反而比vuex还高, 所以技术选型上选用了 vuex, 但是不知道为什么,团队里的一些新人一听 ...
- (三)Python3 循环语句——while
while语句的一般形式: while 判断条件: 语句 同样需要注意冒号和缩进.另外,在 Python 中没有 do..while 循环. 以下实例使用了 while 来计算 1 到 100 的总和 ...
- 【转】4w+1h 教你如何做用户画像
记得14年开始做用户画像的时候,对于用户画像完全没有概念,以为是要画一幅幅图画,经过两年多的学习和理解,渐渐的总结出了一些方法和技巧,在这里就通过4个W英文字母开头和1个H英文字母开头的单词和大家分享 ...
- bash文本查看及处理工具
文本查看及处理工具: wc [OPTION] FILE... -c: 字节数 -l:行数 -w: 单词数 who | w ...
- BZOJ 4809: 皇后
题目大意: n皇后问题,有些格子不能放. 题解: 直接暴力,并不用加优化就能过. 代码: #include<cstdio> using namespace std; int cc,n,an ...
- VC调试入门
概述调试是一个程序员最基本的技能,其重要性甚至超过学习一门语言.不会调试的程序员就意味着他即使会一门语言,却不能编制出任何好的软件.这里我简要的根据自己的经验列出调试中比较常用的技巧,希望对大家有用. ...
- Python第三方库之openpyxl(8)
Python第三方库之openpyxl(8) 饼图 饼图将数据绘制成一个圆片,每个片代表整体的百分比.切片是按顺时针方向绘制的,0在圆的顶部.饼图只能取一组数据.该图表的标题将默认为该系列的标题. 2 ...
- Android自制rom,为update.zip签名
确认已经安装好openssl openssl genrsa -out key.pem openssl req -new -key key.pem -out request.pem openssl x5 ...
- 【Luogu】P1131时态同步(树形DP)
题目链接 甚矣吾衰也!这么简单的DP我都不会了 太恐怖了 树形DP,从子树里选出时间最长的来,剩下的调到这个最长时间即可. #include<cstdio> #include<cct ...
- BZOJ2561 最小生成树 【最小割】
题目 给定一个边带正权的连通无向图G=(V,E),其中N=|V|,M=|E|,N个点从1到N依次编号,给定三个正整数u,v,和L (u≠v),假设现在加入一条边权为L的边(u,v),那么需要删掉最少多 ...