doure5236 2015-03-15 22:52 采纳率: 0%
浏览 37
已采纳

造成这种恐慌的原因是什么?

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.

  • 写回答

2条回答 默认 最新

  • dqys98341 2015-03-15 23:01
    关注

    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)}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何使用simulink建立一个永磁同步直线电机模型?
  • ¥30 天体光谱图的的绘制并得到星表
  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗