go interface{}类型_mismatched types string and interface{}-程序员宅基地

技术标签: golang  

在go语言程序中经常能看到下面这些定义

func errorf(linenum int, format string, args ...interface{
    })

上面的语句interface{}类型代表函数的最终参数可以是任何类型

items map[interface{
    }]*CacheItem

上面的语句interface{}标识map的key可以是任何类型。

下面针对这种写法我们看看《The Go Programming Language》第七章第三节是怎么描述的:

A type satisfies an interface if it possesses all the methods the interface requires. For example, an *os.File satisfies io.Reader, Writer, Closer, and ReadWriter. A *bytes.Buffer satisfies Reader, Writer, and ReadWriter, but does not satisfy Closer becaus e it does not have a Close method. As a shorthand, Go programmers often say that a concrete type “is a”’ particular interface type, meaning that it satisfies the interface. For example, a *bytes.Buffer is an io.Writer; an *os.File is an io.ReadWriter.

如果类型拥有接口所需的所有方法,则类型匹配接口。 例如,*os.File满足io.ReaderWriterCloserReadWriter*bytes.Buffer满足ReaderWriterReadWriter,但不满足Closer,因为它没有Close方法。 作为简写,Go程序员经常说具体类型“是一种”特定的接口类型,这意味着它满足了接口。 例如,* bytes.Buffer是一个io.Writer; *os.File是一个io.ReadWriter

The assignability rule for interfaces is very simple: an expression may be assigned to an interface only if its type satisfies the interface. So:

接口的可分配性规则非常简单:只有当接口的类型满足接口时,才能将表达式分配给接口。 所以:

var w io.Writer
w = os.Stdout  // OK: *os.File has Write method
w = new(bytes.Buffer)  // OK: *bytes.Buffer has Write method
w = time.Second  // compile error: time.Duration lacks Write method
var rwc io.ReadWriteCloser
rwc = os.Stdout // OK: *os.File has Read, Write, Close methods
rwc = new(bytes.Buffer) // compile error: *bytes.Buffer lacks Close method

An interface with more methods, such as io.ReadWriter, tells us more about the values it contains, and places greater demands on the types that implement it, than does an interface with fewer methods such as io.Reader. So what does the type interface{}, which has no methods at all, tell us about the concrete types that satisf y it?

具有更多方法的接口,例如io.ReadWriter,告诉我们更多关于它包含的值,并且对实现它的类型提出了更大的要求,而不是像io.Reader这样的更少方法的接口。 那么没有方法的类型interface{}告诉我们满足它的具体类型是什么?

That’s right: nothing. This may seem useless, but in fac t the type interface{}, which is called the empty interface type, is indispens able. Because the empty interface type places no demands on the types that satisfy it, we can assign any value to the empty interface.

没错:没有任何类型。 这似乎没用,但是在类型interface {}中,它被称为空接口类型,是不可或缺的。 因为空接口类型对满足它的类型没有要求,所以我们可以为空接口分配任何值。

var any interface{
    }
any = true
any = 12.34
any = "hello"
any = map[string]int{
    "one": 1}
any = new(bytes.Buffer)

Of course, having created an interface{} value containing a boolean, float, string , map, pointer, or any other type, we can do nothing directly to the value it holds since the interface has no methods. We need a way to get the value back out again. We’ll see how to do that using a type assertion in Section 7.10.

当然,创建了一个包含布尔值,浮点数,字符串,映射,指针或任何其他类型的interface{}值,由于接口没有方法,我们无法直接处理它所拥有的值。 我们需要一种方法来重新获得值。 我们将在第7.10节中看到如何使用类型断言来做到这一点。

如何重新获得值呢,先看看下面的代码:

	var a interface{
    }
	a = "hello world"
	var buff string
	buff = "BarryLyndon" + a

上面的代码运行会报错:invalid operation: "BarryLyndon" + a (mismatched types string and interface {})
这是类型不匹配的错误,那么如何从新获取a中的实际值呢,将buff = "BarryLyndon" + a语句换成buff = "BarryLyndon" + a.(string)就可以了

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/ranwei693532/article/details/88576108

智能推荐

使用navicat premium远程连接centos7的mysql出现10038错误如何解决_navicate连接虚拟机centos7数据库10038-程序员宅基地

文章浏览阅读4.5k次,点赞2次,收藏10次。对于使用navicat premium工具链接MySQL的一些写错误发,百度上面有很多都是让你在my.ini/my.cnf/my.cnf.d下面删除IP地址,我找了很久都没有找到出现上述情况1、首先 设置远程访问权限 在mysql语句中执行语句grant all privileges on *.* to 'root'@'%' identified by 'youpassword' wi..._navicate连接虚拟机centos7数据库10038

修改el-table-column单独的样式_el-table-column 单独样式-程序员宅基地

文章浏览阅读4.5k次。<template> <el-table :data="tableData" border style="width: 100%" :cell-style="setCellStyle" :header-cell-style="{ textAlign: 'center' }" > <el-table-column prop="date" label="日期" width="180"> </el-table-col_el-table-column 单独样式

Python--day3作业_令dict={1,2,2:3,3:4},dict2={v:u for u,v in dict1.it-程序员宅基地

文章浏览阅读104次。1.集合操作:set_data1 = {1, 2, 3, 4}set_data2 = {2, 3, 5}求两个集合的交集求两个集合的并集求两个集合的差集set_data1 = {1, 2, 3, 4}set_data2 = {2, 3, 5}new = set_data1.intersection(set_data2)print(new)new1 = set_data1.union(set_data2)print(new1)new2 = set_data1.difference(se_令dict={1,2,2:3,3:4},dict2={v:u for u,v in dict1.items() if v>=3},则print(di

利用Asp.net管道优化EntityFramework生命周期管理_entity framework生命周期-程序员宅基地

文章浏览阅读960次。HttpApplication是整个ASP.NET的核心,在第一次请求到抵达后,ASP.NET会创建大量HttpApplication对象置于对象池中并保持其存活。在后续请求的时候,ASP.NET会查看对象池中有无空闲HttpApplication对象,若有则直接使用,若都处于繁忙状态则重新创建。这也就是为什么网站第一次访问的速度很慢。 HttpApplication会不断处_entity framework生命周期

Tcmalloc源码简单分析(5)_tcmalloc初始化源码-程序员宅基地

文章浏览阅读741次。SizeMap初始化完成后,initStaticVars()开始初始化span_allocator_.Init(),并调用两次,主要目的应该是为了能够成功初始化,并保证cache不会存在冲突。;span_allocator_是一个PageHeapAllocator(page_he_tcmalloc初始化源码

visual studio:win7无法在线安装visual studio 2019_vsstudio不支持win7-程序员宅基地

文章浏览阅读427次。C:\Users\用户名\AppData\Local\Temp\VSFaultInfo\xxx(数字序列号)\ErrorInformationError InformationAppInsightsEvent Name = vs/setup/bootstrapper/error Description = Failure to download TelemetrySession = '0eaed727-b5bb-463e-a427-09c956d1b368' Sta..._vsstudio不支持win7

随便推点

python通信工程定额_版通信工程费用及定额套用解读-程序员宅基地

文章浏览阅读134次。通信工程费用定额套用解读一、通信工程总费用构成及内容:是按建设部财政部建标[2003]206号文件的规定做了调整。单项工程总费用由工程费、工程建设其它费、预备费和建设期利息组成。其中:*工程费由建筑安装工程费和设备、工器具购置费组成。*建筑安装工程费由直接费、间接费、利润和税金组成。*直接费由直接工程费和措施费组成。二、建筑安装工程费(见附件一)1、直接工程费:指施工过程中耗用的构成工程实体和有助..._通信工程做了钢槽保护怎么套定额结算

定时任务规则 * * * * * ?详解_定时任务* * * ** 代表什么-程序员宅基地

文章浏览阅读6.9k次,点赞2次,收藏10次。定时任务规则_定时任务* * * ** 代表什么

负载均衡策略和技术的基本指南-程序员宅基地

文章浏览阅读1.8w次。CLB可以根据请求的内容进行流量分发,支持HTTP、HTTPS和TCP协议,适用于Web应用、移动应用和API服务等。:经典负载均衡、应用负载均衡和网络负载均衡。腾讯云和阿里云的负载均衡器都提供自动扩展、健康检查、会话保持、监控报警等功能,可以根据应用的需求选择合适的负载均衡器类型。负载均衡器的一个基本功能是对服务器进行持续的运行状况检查,以确保流量仅定向到在线且响应良好的服务器。在某些配置中,DNS 故障转移可以将流量从不再接受连接(例如发生故障的负载均衡器)的 IP 地址重新路由到预配置的备用 IP。

shell-程序员宅基地

文章浏览阅读7w次,点赞100次,收藏440次。shell什么是shell:现在我们使用的操作系统(Windows、Mac OS、Android、iOS等)都是带图形界面的,简单直观,容易上手,对专业用户(程序员、网管等)和普通用户(家庭主妇、老年人等)都非常适用;计算机的普及离不开图形界面。然而在计算机的早期并没有图形界面,我们只能通过一个一个地命令来控制计算机,这些命令有成百上千之多,且不说记住这些命令非常困难,每天面对没有任何色彩..._shell

【学习笔记】编译原理——第一章 编译引论_设计一个编译器时,非必需的阶段是-程序员宅基地

文章浏览阅读1.5k次,点赞6次,收藏23次。系列文章目录【学习笔记】计算机网络第——第一章 编译引论文章目录系列文章目录前言程序设计语言与编译程序编译器的作用地位:编译程序的分类与执行:编译程序的表示编译程序的逻辑结构编译程序的结构与组织遍(Pass)一遍扫描的编译器两遍扫描的编译器思考题前言提示:这里可以添加本文要记录的大概内容:例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。程序设计语言与编译程序源语言:用来编写源程序的语言,一般是汇编语言或高级程序语言_设计一个编译器时,非必需的阶段是

kkt条件的4个条件解释?-程序员宅基地

文章浏览阅读2.1k次,点赞3次,收藏5次。互补松弛条件要求最优解x*和相应的拉格朗日乘子λ_i的乘积为零。综上所述,KKT条件包括稳定性条件、原始可行性条件、对偶可行性条件和互补松弛条件。当最优解满足这些条件时,它就是约束优化问题的一个潜在最优解。原始可行性条件要求最优解x*满足所有的不等式约束和等式约束。满足了约束条件的梯度为零,即在最优解处,目标函数在可行域内的变化趋势与约束条件保持一致。λ_i * g_i(x*) = 0,对于 i = 1, 2, …g_i(x*) ≤ 0,对于 i = 1, 2, …稳定性条件要求在最优解x。_kkt条件