这种恐慌的原因是什么?

huangapple go评论77阅读模式
英文:

What is the cause of this panic?

问题

我对Go语言还比较新手,能有人帮我诊断一下这个问题吗?

type ValidationStatus struct {
    Passed bool
    Errors map[string]*ValidationError
}

// ...

status := ValidationStatus{Passed: true}

// ...

status.Passed = false
fmt.Println(reflect.TypeOf(typeField.Name)) // string
fmt.Println(reflect.TypeOf(validationError)) // *validation.ValidationError   
status.Errors[typeField.Name] = validationError // 这里引发了恐慌。

validationError 在 validation 包中定义。这段代码与结构体位于同一个文件中。

这是我第一次遇到这样的问题,我觉得可能是我错误地使用了 map,但我不明白为什么这不会导致编译错误,所以可能是类型问题?如果有解决这个问题的指导意见,我将非常感激。

英文:

I am fairly new to Go, could someone help me diagnose this problem.

type ValidationStatus struct {
    Passed bool
    Errors map[string]*ValidationError
}

// ...

status := ValidationStatus{Passed: true}

// ...

status.Passed = false
fmt.Println(reflect.TypeOf(typeField.Name)) // string
fmt.Println(reflect.TypeOf(validationError)) // *validation.ValidationError   
status.Errors[typeField.Name] = validationError // Panic triggered here.

validationError is defined in the validation package. This code is in the same file as the struct.

This is the first time I have hit an issue like this, I think I may be using the map incorrectly but then I don't understand why this wouldn't cause a compile error so maybe a type issue? Any pointers to solve this would be much appreciated.

答案1

得分: 5

你没有告诉我们错误信息是什么!

Map类型

使用内置函数make创建一个新的空映射值,该函数接受映射类型和可选的容量提示作为参数:

make(map[string]int)
make(map[string]int, 100)

例如,

status := ValidationStatus{Passed: true, Errors: make(map[string]*ValidationError)}
英文:

You didn't tell us what the error message was!

> Map types
>
> A new, empty map value is made using the built-in function make, which
> takes the map type and an optional capacity hint as arguments:
>
> make(map[string]int)
> make(map[string]int, 100)

For example,

status := ValidationStatus{Passed: true, Errors: make(map[string]*ValidationError)}

答案2

得分: 3

你的地图是nil。你只需要初始化它。这就是为什么大多数对象初始化都隐藏在一个函数后面:

status := ValidationStatus{Passed: true, Errors: make(map[string]*ValidationError)}

或者,隐藏在一个函数后面:

status := NewValidationStatus()

// ...

func NewValidationStatus() ValidationStatus {
    return ValidationStatus{
        Passed: true,
        Errors: make(map[string]*ValidationError),
    }
}

在 playground 上查看

英文:

Your map is nil. You simply need to initialize it. This is why most object initialization is hidden behind a function:

status := ValidationStatus{Passed: true, Errors: make(map[string]*ValidationError)}

..or, behind a function:

status := NewValidationStatus()

// ...

func NewValidationStatus() ValidationStatus {
    return ValidationStatus{
        Passed: true,
        Errors: make(map[string]*ValidationError),
    }
}

<kbd>See it on the playground</kbd>

huangapple
  • 本文由 发表于 2015年3月16日 06:52:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/29067168.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定