我是Go的新手,想要在go中创建和初始化struct数组.我的代码是这样的
type node struct {
name string
children map[string]int
}
cities:= []node{node{}}
for i := 0; i<47 ;i++ {
cities[i].name=strconv.Itoa(i)
cities[i].children=make(map[string]int)
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
panic: runtime error: index out of range
goroutine 1 [running]:
panic(0xa6800, 0xc42000a080)
Run Code Online (Sandbox Code Playgroud)
请帮忙.TIA :)
我正在寻找一种使用 Go 为每个键存储多个值的方法(就像我们在 python 中使用字典一样)。Go 有没有办法实现这一点?
安装以下命令时,我收到错误,如下所示
parag@parag:~/torch-hdf5$ sudo luarocks make hdf5-0-0.rockspec LIBHDF5_LIBDIR="/usr/lib/x86_64-linux-gnu/"
Missing dependencies for hdf5:
totem
Error: Could not satisfy dependency: totem
Run Code Online (Sandbox Code Playgroud)
图腾已经安装好了.
parag@parag:~$ sudo apt-get install totem
[sudo] password for parag:
Reading package lists... Done
Building dependency tree
Reading state information... Done
totem is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 102 not upgraded.
parag@parag:~$
Run Code Online (Sandbox Code Playgroud)
我现在该怎么办?
我在Go中有一个函数,我希望使用gob编码的返回值.返回值是结构指针.然而,即使我确实了解导出变量是什么,我也不太确定如何使其工作.
这就是我的功能
fun loadXYZ(root *structABC) *structABC{
const once = "stateData.bin"
rd, errr := ioutil.ReadFile(once)
if errr!=nil{
//Do some computation and store in "root"
buf := &bytes.Buffer{}
errr = gob.NewEncoder(buf).Encode(root)
if errr != nil {
panic(errr)
}
errr = ioutil.WriteFile(once, buf.Bytes(), 0666)
if errr != nil {
panic(errr)
}
return root
}
var d *structABC
errr = gob.NewDecoder(bytes.NewReader(rd)).Decode(&d)
if errr != nil {
panic(errr)
}
return d
}
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误
panic: gob: type main.stateNode has no exported fields
Run Code Online (Sandbox Code Playgroud)
我知道错误发生的原因.但有人可以帮我解决吗?