为什么在函数中传递结构体指针时会出现空指针异常?
来源:stackoverflow
时间:2024-02-19 13:27:27 133浏览 收藏
从现在开始,努力学习吧!本文《为什么在函数中传递结构体指针时会出现空指针异常?》主要讲解了等等相关知识点,我会在golang学习网中持续更新相关的系列文章,欢迎大家关注并积极留言建议。下面就先一起来看一下本篇正文内容吧,希望能帮到你!
问题内容
问题: 我有一个包含一个地图和两个切片的结构。当我传递指针值时,无论我做什么,结构中的这些集合始终为零(即无法将任何内容附加到地图或切片)。
详细信息: 我不明白为什么会发生这种情况。我做了一个看似无关的更改(我不记得了),现在没有任何内容会附加到结构中的 map 中。我将 map 指针传递给后续函数,但它不断抛出错误“恐慌:分配给 nil 映射中的条目”。
我传递了 linkresource 和 linkreport 类型值的指针,但它总是说映射为零。我什至使用 make(...) 强制将 linkreport 结构值清零作为调试测试,但是当该值传递给后续函数时,它仍然声称 map 和 slices 为零;这怎么可能?我该如何纠正它?
链接资源
package model
type linkresource struct {
id int
url string
health string
status int
message string
}
链接报告
type linkreport struct {
summary map[string]int
brokenlinks []linkresource
unsurelinks []linkresource
}
抛出错误的逻辑
type linkservice struct { }
func (linkservice *linkservice) populatelinkreportmetadata(responsecode int, message string, health string, link *model.linkresource, linkreport *model.linkreport) {
linkreport.summary[health]++
link.message = message
link.health = health
link.status = responsecode
switch health {
case constants.broken:
linkreport.brokenlinks = append(linkreport.brokenlinks, *link)
break
case constants.unsure:
linkreport.unsurelinks = append(linkreport.unsurelinks, *link)
break
}
}
测试函数以重现问题
func TestPopulateLinkReportMetadata(t *testing.T) {
link := model.LinkResource{}
linkReport := model.LinkReport{}
link.ID = 1234
link.URL = "http://someurl.com"
linkService := LinkService{}
t.Log("After analyzing a LinkResource")
{
linkService.populateLinkReportMetadata(http.StatusOK, link.Message, constants.Healthy, &link, &linkReport)
if linkReport.Summary[constants.Healthy] > 0 {
t.Logf("The LinkResource should be appended to the %s summary slice %s", constants.Healthy, checkMark)
} else {
t.Fatalf("The LinkResource should be appended to the %s summary slice %s", constants.Healthy, ballotX)
}
}
}
感谢您的帮助,因为我很困惑,谢谢!
解决方案
这是一个简单的错误示例:
type linkreport struct {
summary map[string]int
}
func main() {
var link linkreport
// panic: assignment to entry in nil map
link.summary["month"] = 12
}
和一个简单的修复:
var link linkreport link.summary = make(map[string]int) link.summary["month"] = 12
或者你可以创建一个“新”函数:
func newLinkReport() linkReport {
return linkReport{
make(map[string]int),
}
}
func main() {
link := newLinkReport()
link.summary["month"] = 12
}理论要掌握,实操不能落!以上关于《为什么在函数中传递结构体指针时会出现空指针异常?》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!
声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
最新阅读
更多>
-
467 收藏
-
Golang · Go问答 | 1小时前 | EventSource · sse · Go问答 · http.Flusher · 实时推送 · EventSource FLUSH text/event-stream Go问答 http.Flusher Go SSE333 收藏
-
Golang · Go问答 | 1小时前 | 并发安全 · RWMutex · sync.Map · Go问答 · map并发 · RWMutex sync.Map Go问答 Go map并发读写 concurrent map writes102 收藏
-
166 收藏
-
Golang · Go问答 | 5天前 | 并发 · channel · select · 性能排查 · Go问答 · select Go channel context default CPU飙高 忙等循环 ticker438 收藏
-
Golang · Go问答 | 5天前 | pprof · trace · 性能排查 · Go问答 · 服务安全 · Go pprof 生产环境 trace 安全入口 net/http/pprof 性能排障349 收藏
-
Golang · Go问答 | 5天前 | channel · 并发编程 · Go问答 · 背压 · 容量规划 · Goroutine channel 缓冲区 背压 Go问答 buffered channel 并发容量377 收藏
-
Golang · Go问答 | 5天前 | interface · 单元测试 · 架构设计 · repository · Go问答 · 单元测试 架构设计 interface 接口设计 Go问答 调用方定义 Repository212 收藏
-
Golang · Go问答 | 5天前 | JSON · time.Time · 接口设计 · Go问答 · encoding/json · encoding/json API响应 JSON序列化 time.Time omitempty Go问答 omitzero315 收藏
-
Golang · Go问答 | 5天前 | HTTP · Cookie · 浏览器 · cors · Go问答 · SameSite · cookie cors Secure SameSite Go问答 Set-Cookie 跨站请求 credentials246 收藏
-
Golang · Go问答 | 5天前 | 中间件 · Context · Go问答 · 架构模式 · 代码边界 · 中间件 context Context.Value Go问答 WithValue 请求作用域 业务参数269 收藏
-
Golang · Go问答 | 5天前 | JSON · 后端开发 · Go问答 · encoding/json · 接口解析 · JSON解析 encoding/json DisallowUnknownFields Go问答 RawMessage json.Decoder UseNumber151 收藏
课程推荐
更多>
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 485次学习