草庐IT

extend-anonymous-types-using

全部标签

string - 无法在字符串 : "cannot use <xxx> (type <yyy>) as type string in map index" 的映射中使用基础类型的字符串

这个问题在这里已经有了答案:ConvertingacustomtypetostringinGo(4个答案)关闭3年前。我有底层字符串类型:typeCapabilitystring。我想将它用作字符串映射中的字符串,但出现错误:cannotusecap(typeCapability)astypestringinmapindex这是我的代码:packagemainimport("fmt")typeCapabilitystringvarcaps_list=map[string]int{"HOME":1,}funcmain(){varcapCapability//stringcap="HOME

go - 为什么不能在赋值中使用 (type func(string)) 作为 type func(interface{})

这个问题在这里已经有了答案:Typefuncwithinterfaceparameterincompatibleerror(1个回答)Funcwithinterfaceargumentnotequalstofuncwithstringargument.Why?(1个回答)Gofunctiontypesthatreturnstructsbeingusedwithinterfaces(2个答案)PassinganarbitraryfunctionasaparameterinGo(4个答案)Howtoconvertfrom`func()*int`to`func()interface{}`?[

戈朗 :which way is more efficient about using "for range"

typepath[]bytefunc(ppath)ToUpper(){fori,b:=rangep{if'a'在上面(这个例子是从“TheGoBlog”复制过来的),如果ToUpper变成这样:func(ppath)ToUpper(){fori,_:=rangep{if'a'哪个会更有效率为什么?“TheGoBlog”对前一个说:“这里的ToUpper方法在forrange构造中使用两个变量来捕获索引和slice元素。这种形式的循环避免了在主体中多次写入p[i]。”什么意思? 最佳答案 前者有更多的内存操作,即在b上:它在循环的第一

go - "Declared but not used"for条件表达式中的变量

关闭。这个问题需要debuggingdetails.它目前不接受答案。编辑问题以包含desiredbehavior,aspecificproblemorerror,andtheshortestcodenecessarytoreproducetheproblem.这将有助于其他人回答问题。关闭6年前。Improvethisquestion我的直觉方法:https://play.golang.org/p/6xzg7TK1IH它不起作用。你能分享一些替代方法吗?

go - 新的 TCP 连接抛出 "use of closed network connection",即使它刚刚打开

打开新连接时,我有时会在client.Socket.Read(message)处收到“使用已关闭的网络连接”错误在receive功能。这已经够奇怪了,但真正奇怪的是我在尝试关闭位于close(connection.Data)的已关闭channel时收到错误消息,当客户端通过manager.unregister.我显然在这里遗漏了一些东西,因为我的所有日​​志记录都表明这些是全新且未使用的channel和套接字。有什么想法吗?我使用的是Golang1.10版。typeClientManagerstruct{clientsmap[*types.Client]boolbroadcastcha

go - 如何通过 (type *common.MapStr) 来输入 []byte?

对不起,如果这个问题太新手,因为我昨天才开始学习围棋。我尝试将publishEvent转换为字节,编译器显示如下错误:cannotconvertpublishEvent(type*common.MapStr)totype[]byte谁能给我指路?谢谢。varparsedmap[string]interface{}bytes:=[]byte(publishEvent)--->Erroroccurhereerr:=json.Unmarshal(bytes,&parsed)iferr!=nil{fmt.Println("error:",err)} 最佳答案

go - golang中如何判断unmarshal json interface{} type?

我想判断json类型,但是总是返回"Idon'tknowabouttypemap[string]interface{}!",如何解决。=======================================================================typegetRemoteCardInfostruct{CodeintMsgstringData[]*remoteCardInfo}typeremoteCardInfostruct{SnstringRemoteCardIpstringRemoteCardMacstring}funcGet_json_data(url

arrays - 如何在 golang 中使用 type.NewArray 初始化数组?

基本上,我想在types模块中使用Array类型结构,但我无法对其进行初始化。您必须将什么作为第一个参数传递给类型?packagemainimport("fmt""go/types")funcmain(){vara*types.Arraya=types.NewArray(types.Int,2)//errorherefmt.Println(a)} 最佳答案 我也是新手。对我来说,这个问题看起来并不愚蠢。不幸的是,还没有人解释如何使用NewArray。在我看来,golang社区不是很友好我现在遇到的反对票多于真正的帮助。顺便说一句,我

go - 错误 : "declared and not used" even if I assign to it

我找不到为什么下面的代码给出编译错误“alivedeclaredandnotused”。funcping(ipstring){varalivebool_,err:=exec.Command("ping","-n1","-w1000",ip).Output()iferr!=nil{alive=false}else{alive=true}} 最佳答案 您看到的编译错误正是正在发生的事情。varalivebool未使用。您声明它并为其分配一个值,但您永远不会对它做任何事情。这是对将运行的代码的playground友好修改:packagem

pointers - 初始化指针数组 - 无法创建类型 *[]type

它是一个简单的代码,将car类型的对象保存到一个car类型的数组中。我在这里尝试使用指针来传递数组引用。有2个问题:1-我无法将其初始化为空数组。当我使用make时,它说:cannotmaketype*[]car2-如果我不使用make,会出现运行时错误:panic:runtimeerror:invalidmemoryaddressornilpointerdereference代码:import"fmt"typecarstruct{platestringcolorstring}funcmain(){var_cars*[]car//listofcars_cars=make(*[]car,