草庐IT

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

coder 2024-07-09 原文

<分区>

我必须结构让我们说 struct1 和 struct2,struct2 包含一个带有 struct1 的映射,struct1 也包含一个映射,我想更改 struct1 中存在的映射。

这是抛出一个运行时错误: panic :运行时错误:无效内存地址或零指针解引用

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...)   
        }
}

有关Go Error : panic: runtime error: invalid memory address or nil pointer dereference. Changing map inside a struct which is present in 另一个结构,的更多相关文章

随机推荐