我不明白为什么在附加到另一个结构内的结构切片时会出现无效的内存地址恐慌。
运行代码时出现以下错误。
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x0 pc=0x400d5f]
goroutine 3 [running]:
main.Pairs.CollectTickers(0x0, 0x0)
test.go:32 +0x15f
created by main.main
test.go:42 +0x42
goroutine 1 [sleep]:
time.Sleep(0x174876e800)
/usr/lib/go/src/pkg/runtime/ztime_linux_amd64.c:19 +0x2f
main.main()
test.go:43 +0x57
exit status 2
此代码产生以下错误:
package main
import (
"fmt"
"sync"
"time"
)
var PairNames = []string{ "kalle", "kustaa", "daavid", "pekka" }
type Data struct {
a int
b int
}
type Tickers struct {
Tickers []Data
}
type Pairs struct {
Pair map[string]*Tickers
Mutex sync.Mutex
}
func (pairs Pairs) CollectTickers() {
PairCount := len(PairNames)
for x := 0; x <= 1000; x++ {
for i := 0; i < PairCount-1; i++ {
var data Data
data.a = i * x
data.b = i + x
pairs.Mutex.Lock()
pairs.Pair[PairNames[i]].Tickers = append(pairs.Pair[PairNames[i]].Tickers, data)
pairs.Mutex.Unlock()
fmt.Printf("a = %v, b = %v\r\n", data.a, data.b)
}
}
}
func main() {
var pairs Pairs
go pairs.CollectTickers()
time.Sleep(100 * time.Second)
}
喵喔喔
相关分类