我試圖弄清楚如何以正確的方式訪問地圖的結構檔案。
我有兩個結構
type ZipStructure struct {
Pfx []byte
Cert []byte
Key []byte
Json []byte
}
type ServerConfig struct {
ClusterSetupZip map[string]ZipStructure
}
我想為地圖分配空間并訪問ZipStructure.
例如, zs[DirName].Pfx = bytes
只有當我將宣告更改map為map[string]*ZipStructure
雖然我得到了 NRE
我也試圖以這種方式分配空間make(map[string]*ZipStructure,4)` 也沒有幫助。
uj5u.com熱心網友回復:
如果要訪問存盤映射中的結構體,可以檢查它是否存在,然后創建它:
if _, found := zs[DirName]; !found {
zs[DirName] = &ZipStructure{
//If you need, you can initialize the slices also here
Pfx: make([]byte,0),
Cert: make([]byte,0),
Key: make([]byte,0),
Json: make([]byte,0),
}
}
//Now its accessible
zs[DirName].Pfx = append(zs[DirName].Pfx,b)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/369731.html
標籤:走
