1 回答

TA貢獻(xiàn)1872條經(jīng)驗(yàn) 獲得超4個(gè)贊
您編寫的代碼在編譯時(shí)不會(huì)彈出 nil 錯(cuò)誤。它只會(huì)在您以錯(cuò)誤的方式使用它時(shí)導(dǎo)致 nil 點(diǎn)錯(cuò)誤。
failuresInCommits map[string][]string你后來用了嗎make()?
Failures map[git_repo.FilePath]*FailureData你在'make()'之后使用過這個(gè)嗎?
好的,現(xiàn)在你專注于 ok := r.Failures[filePath].failuresInCommits,你確定r.Failures[filePath]返回'failuresIncommits,true',
如果不是,那么 r.Failures[filePath] 是 nil,好吧,你告訴我什么是nil.failuresInCommits.
還有一個(gè)風(fēng)險(xiǎn)是你只能在這個(gè)特定的包中使用 x.failureInCommits。如果你在其他一些包中做同樣的事情,x.failureInCommits 將無法訪問,因?yàn)樽侄涡懴拗啤?/p>
怎么做 ?
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)
}
- 1 回答
- 0 關(guān)注
- 166 瀏覽
添加回答
舉報(bào)