草庐IT

map函数

全部标签

dictionary - 从 map[string]string 获取单个键值

我的cookie中存储了一张map,如下所示:cookieData:=map[expiration:1533455712ip:[hash:dd363d13234566727743277e96email:user@user.com]fmt.Println(reflect.TypeOf(cookie))>>map[string]string有人可以帮助我了解如何从这张map中只获取电子邮件值吗?提前致谢。 最佳答案 golang中的类型转换是通过附加.(T)完成的ip:=cookieData["ip"].(map[string]stri

go - 具有返回值的go函数

我看到了这段代码,并试图理解这一行func(ml*MarketList)UnmarshalJSON(b[]byte)error在上面的代码行中,我相信函数名称是UnmarshallJSON,它将字节数组作为输入,并将错误返回作为输出。是什么func(ml*MarketList)我知道*作为取消引用的运算符,但不了解这里的上下文。如果有人可以解释,那将是很好的。packagemainimport("encoding/json""fmt")varjsonBytes=[]byte(`{"MS":{"last":"25","highestBid":"20"},"GE":{"last":"24"

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 - 将闭包作为函数的参数传递

来自这个例子:https://gobyexample.com/closures如果我们改变:fmt.Println(nextInt())fmt.Println(nextInt())fmt.Println(nextInt())到fmt.Println(intSeq())fmt.Println(intSeq())fmt.Println(intSeq())gorun将失败并出现错误:./prog.go:32:5:PrintlnargintSeq()isafuncvalue,notcalled但是从这个例子来看:https://gobyexample.com/recursionfmt.Prin

go - 在 Go 中调用一个将接口(interface)作为参数的函数

关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion如何构造接口(interface)作为函数的参数?typeblahinterfaceinterface{method1()method2()method3()}funcblah(iblahinterface){}blah(?)

go - 导出名称以小写字母开头的函数

只是好奇,有没有办法导出名称以小写字符开头的函数,例如“print”或“start”?旁注:就像您使用JSON一样:typeTstruct{FieldAint`json:"field_a"`FieldBstring`json:"field_b,omitempty"`} 最佳答案 不,没有。Golanguagespecification明确指出:ExportedidentifiersAnidentifiermaybeexportedtopermitaccesstoitfromanotherpackage.Anidentifierise

go - 确定从 go 函数返回的内容

鉴于此功能:func(c*Firehose)PutRecord(input*PutRecordInput)(*PutRecordOutput,error){req,out:=c.PutRecordRequest(input)returnout,req.Send()}我发现这个调用有效:err,_:=svc.PutRecord(putRecordInput)但是我仍然不太清楚这在函数签名中意味着什么:(*PutRecordOutput,error)我的问题是,我能否始终根据返回行中指定的内容来确定函数返回的内容,在本例中为:返回,req.Send() 最佳答案

arrays - 在 golang 中创建 map 数组的 map

我想在golang中创建一个json,我需要首先为其创建以下映射:{"inputs":[{"data":{"image":{"url":"SOME_URL"}}}]}如何在golang上创建这个map。(现在即使硬编码也适用于我) 最佳答案 在结构中:typeSomeDatastruct{Inputs[]struct{Datastruct{Imagestruct{URLstring`json:"url"`}`json:"image"`}`json:"data"`}`json:"inputs"`}但是如果我们希望能够单独添加东西,并且

go - 如何根据我发送给 Go 中的函数的变量返回特定类型的 slice

我有一个函数,它接受一个空接口(interface)(任何类型,我正在寻找特定的2),然后返回所选类型的一部分。functestingInterface(tempinterface{})(interface{},interface{}){vardocinterface{}array:=make([]interface{},3)switchx:=temp.(type){caseint:doc=xtempArray:=make([]string,3)fori,v:=rangetempArray{array[i]=string(v)}fmt.Printf("Inttostring%T,%T"

sql - Golang 中函数的泛化

我想编写一个处理SQL查询的通用函数。Sqlx模块提供函数StructScan(),自动将结果扫描到struct字段中。typePlacestruct{CountrystringCitysql.NullStringTelephoneCodeint`db:"telcode"`}rows,err:=db.Queryx("SELECT*FROMplace")forrows.Next(){varpPlaceerr=rows.StructScan(&p)}因此,建议的函数签名如下所示:funcQuery(db*sql.DB,query){rows,err:=db.Queryx("SELECT*F