简体   繁体   中英

Go Error: panic: runtime error: invalid memory address or nil pointer dereference. Changing map inside a struct which is present in another struct,

I have to structs lets say struct1 and struct2, struct2 contains a map with struct1, struct1 also contains a map, I want to change the map which is present in struct1.

This is throwing a runtime error: panic: runtime error: invalid memory address or nil pointer dereference

type FailureData struct {
    failuresInCommits map[string][]string 
}

type DetectionResults struct {
    Failures map[git_repo.FilePath]*FailureData
        //Have other things
}

func (r *DetectionResults) Fail(filePath git_repo.FilePath, message 
           string, commits []string) {  
        ok := r.Failures[filePath].failuresInCommits //error occurs here    
        if r.Failures[filePath].failuresInCommits == nil {       
            r.Failures[filePath].failuresInCommits = make(map[string][]string)
        }
        if len(ok) == 0 {
            r.Failures[filePath].failuresInCommits[message] = commits
        } else {
            r.Failures[filePath].failuresInCommits[message] = 
               append(r.Failures[filePath].failuresInCommits[message], 
                      commits...)   
        }
}

The code you write will not pop nil error while compiling.It only causes nil point error when you are using it in a wrong way.

failuresInCommits map[string][]string did you use it after make() ?

Failures map[git_repo.FilePath]*FailureData did you use this after 'make()'?

ok , now you focus on ok := r.Failures[filePath].failuresInCommits ,did you make sure r.Failures[filePath] returns 'failuresIncommits, true',

if not, then r.Failures[filePath] is nil, ok, you tell me what is nil.failuresInCommits .

there is also a risk that you can only x.failureInCommits in this specific package. If you do the same in some other package, x.failureInCommits will be unaccessable becaues of the field lowwer case limited.

How to make ?

package main

type FilePath string

type FailureData struct {
    failuresInCommits map[string][]string
}

func NewFailureData() FailureData {
    return FailureData{
        failuresInCommits: make(map[string][]string, 0),
    }
}
func (fd *FailureData) Set(k string, v []string) {
    fd.failuresInCommits[k] = v
}

type DetectionResults struct {
    Failures map[FilePath]*FailureData
    //Have other things
}

func NewDetectionResults() *DetectionResults {
    return &DetectionResults{
        Failures: make(map[FilePath]*FailureData, 0),
    }
}
func (dr *DetectionResults) Set(fp FilePath, fd *FailureData) {
    dr.Failures[fp] = fd
}

func main() {
    fd := NewFailureData()
    dr := NewDetectionResults()
    comments := []string{"push error", "commit error", "add error"}

    fd.Set("hash-18ef8abujce8fad0h8j", comments)
    dr.Set("/etc/go/gopath/src/github.com/xxx/xxx/main.go: 12", &fd)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM