草庐IT

map_num_to_struct

全部标签

http - 向 Struct 添加一些日期,然后将其放入 go 模板中

我有一个文件controllers/catalog.go,它包含一个HTTP处理程序:funcCatalog(whttp.ResponseWriter,r*http.Request){ifr.Method!="GET"{http.Error(w,http.StatusText(405),http.StatusMethodNotAllowed)return}categories,err:=models.GetCategories()iferr!=nil{http.Error(w,http.StatusText(500),http.StatusInternalServerError)ret

json - 在 Golang Structs 和 Json 之间动态转换

我想知道是否有一种方法可以在不使用数组的情况下动态扩展结构中共享相同数据类型的条目数。例如:typeMyHousestruct{Bedroom*Bedroom`json:"bedroom"`Kitchen*Kitchen`json:"Kitchen"`}typeKitchenstruct{Sink*Sink`json:"sink"`Oven*Oven`json:"oven"`}typeOvenstruct{Brandstring`json:"brand"`HobSize[]int`json:"hobs"`typeSinkstruct{Volumeint`json:"volume"`}t

go - 为什么要去 json.Unmarshal auto convert interface{} to map

程序会收到很多msg,msg有不同的struct“Data”,所以我定义了Msg结构体:typeMsgstruct{MsgTypeintDatainterface{}}typeData1struct{//msgtype1Datastruct}typeData2struct{//msgtype2Datastruct}func(msgStrstring){msg:=Msg{}iferr:=json.Unmarshal([]byte(msgStr),&msg);err!=nil{//logerr}switchmsg.MsgType{case1://convertmsg.Datatoatype

go - 附加到golang中的struct slice

我努力寻找我的例子,但尽管一堆问题非常相似,但我无法理解我做错了什么。我对golang很陌生,我正在尝试实现生活游戏。这是我的部分代码//SpeciesstructtypeSpeciesstruct{xPosint32yPosint32isAliveboolwillChangeStateboolrectsdl.Rectneighbours[]Species}typeEcosystemstruct{community[]Species}func(ecos*Ecosystem)addSpecies(spSpecies){ecos.community=append(ecos.communit

go - 我应该在 Go 中封装 slice 和 maps 吗?如果可以,该怎么做?

我想创建一个可以在其他包中访问的结构,但我不想允许修改这个结构。在其他语言中,这是通过将所有字段设为私有(private)并仅公开公共(public)setter/getter来存档的。使用getter的解决方案适用于除slice和映射之外的所有数据类型,因为默认情况下不会复制返回的slice和映射,因此可以修改它们。我想出的唯一解决方案是创建新的map/slice并在循环中分配所有项目,但这会引入大量重复且丑陋的代码,尤其是对于大型嵌套结构。packagemainimport("fmt")typeOtherStructstruct{propstring}typeStructstruc

go - go中修改 map 的约定

在go中,通过重新分配值或使用指针值来修改映射更符合惯例吗?typeFoostruct{Barint}重新分配:foos:=map[string]Foo{"a":Foo{1}}v:=foos["a"]v.Bar=2foos["a"]=v与指针foos:=map[string]*Foo{"a":&Foo{1}}foos["a"].Bar=2 最佳答案 您可能(不经意地)混淆了这里的内容。在map中存储指针的原因不是为了使“点域”修改起作用——而是为了保留map“保存”的值的准确位置。Go映射的一个关键属性是绑定(bind)到它们的键的

戈朗 : How do you use a pointer on a struct that hasn't been initialized yet

所以我在看filehere.他们调用record:=&accessLog但他们从来没有首先将其初始化为变量,如果他们这样做,如果有多个同时连接,记录是否有可能被覆盖用别人的数据?typeaccessLogstruct{ip,method,uri,protocol,hoststringelapsedTimetime.Duration}funcLogAccess(whttp.ResponseWriter,req*http.Request,durationtime.Duration){clientIP:=req.RemoteAddrifcolon:=strings.LastIndex(cli

struct - 在 golang 结构中转换字符串

我有一个AES加密secret的json文件。结构是:{"username":"asdf123ASLdf3","password":"elisjdvo4etQW"}还有一个结构来保存这些值typeSecretsstruct{Usernamestring`json:"username"`Passwordstring`json:"password"`}将加密的json值加载到结构中很容易,但我真正想要的是具有未加密值的结构。因此,对于每个值,我想通过一个函数运行它:aesDecrypt(keystring,valuestring)字符串我很高兴在第一次加载时完成此操作,或者将所有内容移至新

go - 通过给键从 map[string]interface{} 调用函数

我希望能够将函数名称传递给gin.Engine路由处理程序。我有以下代码;//statusservicetypeStatusServicestruct{App*gin.Engine}func(s*StatusService)Ping(ctx*gin.Context){ctx.JSON(200,gin.H{"message":"pong",})}app:=gin.Default()//defineservicesstatusService:=&services.StatusService{App:app,}ss:=make(map[string]interface{})ss["auth"

json - 在 go 中解码 JSON 结构制作空 map

这个问题在这里已经有了答案:PrintingEmptyJsonasaresult[duplicate](1个回答)关闭9个月前。我有一个json文件,其中包含大量数据:{"elec":{"s20":{"coldS":{"wDay":{"Night":{"avg":1234,"stddev":56},"Morning":{"avg":5432,"stddev":10}...},...},...},...}我想将这个文件加载为一个go结构:typeConsumConfigstruct{elecmap[string]map[string]map[string]map[string]Consu