草庐IT

int_types

全部标签

go - 传播时 "Cannot use variable of type []struct as []interface"

这个问题在这里已经有了答案:sliceofstruct!=sliceofinterfaceitimplements?(6个答案)关闭8个月前。原型(prototype)函数functest(i...interface{}){//Codehere}预期用途typefoostruct{//Fields}foos:=[]foo{//foo1,foo2...}test(foos...)//ERRORtest(foos[1],foos[2],...)//OK错误cannotusefoos(variableoftype[]foos)as[]interface{}valueinargumenttot

go - type <Name> <dataType> 和 type <Name> = <dataType> 有什么区别

我是Golang的新手。抱歉,我仍然对以下两者之间的区别感到困惑:type和type=这是一个例子:packagemainimport"fmt"funcmain(){var(strWordWordstrTextText)strWord="gopher"strText="golang"fmt.Printf("strWord=%s,TypeofValue=%T\n",strWord,strWord)fmt.Printf("strText=%s,TypeofValue=%T\n",strText,strText)}typeWordstringtypeText=string输出strWord=

python - Golang 与 Python - 十六进制字符串到 Int

我有一个十六进制字符串:n="0xd458985bc81e284609dd69267c73b8464e1795d5b91ce6ed8871ecbc5b6ec4d1"我可以使用以下方法在python中转换为int:mynum=int(n,16)我得到了长号:96046857981227695367604088053507399752198003710848334588478940192231467697361现在我将如何在Golang中执行此操作? 最佳答案 这是一个很好的问题(尽管与Flimzy发现的另一个问题相似)。主要问题是内置

go - 如何通过扩展类型向 int 这样的基本类型添加功能?

我希望能够向现有类型(例如int)添加方法:func(i*int)myfunction{...}但是,这显然会产生错误。cannotdefinenewmethodsonnon-localtypeGoogle的最高搜索结果是githubissue正是因为这件事而反对golang。令人欣慰的是,答案是您已经可以通过其他方式获得此功能,因此他们不会对语言进行此更改。无益,react含糊typeextendedExisting并且它没有明确显示如何实现OP要求的内容,即:func(aint)sum(bint)(totalint){total=a+breturn}那么,如何扩展一个int来添加功

go - 为什么我不能在 go 中将 `func() []int` 作为 `func() []interface{}` 传递?

我有以下定义:func(c*Collector)RegisterSource(ffunc()[]interface{}){c.source=f}我尝试按如下方式调用它但出现错误:funcsource()[]int{return[]int{0,1,2,3,4}}...c.RegisterSource(source)这会遇到:cannotusesource(typefunc()[]int)astypefunc()[]interface{}inargumenttoc.RegisterSource 最佳答案 relevantGoFAQent

go - *[]Type 和 []*Type 在 go 中有什么区别

假设我们有一个名为Person的结构,它由一个名为People的结构持有。typePerson{Namestringageint}typePeople{CitystringList[]*Person//checkthisout}typePeople2{CitystringList*[]Person//What'sthedifference?}[]*Person和*[]Person到底是什么意思?如何获取这些slice的元素值?我比较熟悉C,所以如果你能用C解释一下,我将不胜感激 最佳答案 []*Type是指向Type的指针片段。*[

go - Type.Field 和 Value.Field 有什么区别?

以下代码。funcfieldsTest(targetinterface{})([]field,error){s:=reflect.ValueOf(target)s=s.Elem()targetType:=s.Type()fori:=0;i如果目标接口(interface)是struct,f的返回值和structField一样吗? 最佳答案 Type.Field()返回reflect.StructField类型的值,和Value.Field()返回reflect.Value类型的值.所以它们不能相同。Type.Field()返回描述字

go - 在 go lang bytes 包中找不到 NewBuffer([]bytes,int,int64) 方法

我是go语言的新手。我试图了解内部发生的事情ioutil.ReadAll(rReader,capacityint64)方法。在这个方法中有一行:buf:=bytes.NewBuffer(make([]byte,0,capacity))但是问题是在bytes包里面有一个只有参数的NewBuffer方法比如:funcNewBuffer(buf[]byte)*Buffer我搜索了bytes一次又一次地打包文档,但找不到带有3个参数的NewBuffer方法。那么实际上从哪里调用NewBuffer(make([]byte,int,int64))方法? 最佳答案

go - 加朗 : type interface {} does not support indexing

我收到以下错误,当我尝试将map添加到界面时:无效操作:msgs["Application"]["instance-id"](类型接口(interface){}不支持索引)应用:resultChannel:=make(chanmap[string]interface{})clients:=make(map[string][]map[string]interface{})gofunc(clientsmap[string][]map[string]interface{}){for{msgs:=0{clientMap:=map[string]interface{}{"instance-id"

pointers - 'myVariable.(type)' 究竟返回什么?

我创建了一个简单的代码packagemainimport("fmt")funcmain(){a:=5b:=&aTest(b)fmt.Println(a)fmt.Println(*b)}funcTest(resultinterface{}){switchr:=result.(type){case*int:*r=10}}你可以运行它here在switch语句内的测试方法中,我创建了一个新变量,它是我的参数类型。为什么我的变量“b”会在更新此指针后更新。为什么这个新变量指向旧变量?程序执行结果为1010但在意料之中55更新我想把问题说清楚。我没有在测试中将指向“b”的指针分配给变量“r”。我