追加到另一个结构内部的结构片时,无效的内存地址崩溃

I do not understand why I get invalid memory address panic when appending to slice of structures inside another structure.

I get the following error when running the code.

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

This code produces the following error:

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
", data.a, data.b)
                }
        }
}


func main() {
        var pairs Pairs
        go pairs.CollectTickers()
        time.Sleep(100 * time.Second)
}

The error is at:

pairs.Pair[PairNames[i]].Tickers = append(pairs.Pair[PairNames[i]].Tickers, data)

Tickers is a nil pointer, since you haven't yet allocated a Tickers instance.

I'm not sure if this is what you intended, but it compiles and runs successfully.

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()
            name := PairNames[i]
            if t, ok := pairs.Pair[name]; ok {
                t.Tickers = append(t.Tickers, data)
            } else {
                pairs.Pair[name] = &Tickers{
                    Tickers: []Data{data},
                }
            }
            pairs.Mutex.Unlock()
            fmt.Printf("a = %v, b = %v
", data.a, data.b)
        }
    }
}

func main() {
    var pairs = Pairs{
        Pair: make(map[string]*Tickers),
    }
    go pairs.CollectTickers()
    time.Sleep(1 * time.Second)
}

The value of pairs.Pair[PairNames[i]] is nil because there is no entry for PairNames[i] in the pairs.Pair map and nil is the zero value for *Tickers. Therefore, pairs.Pair[PairNames[i]].Tickers is an invalid attempt to dereference a nil pointer.

The Go Programming Language Specification

Index expressions

For a of map type M:

if the map is nil or does not contain such an entry, a[x] is the zero value
for the value type of M

Go 1.2 Release Notes

Use of nil

The language now specifies that, for safety reasons, certain uses of nil pointers are guaranteed to trigger a run-time panic.