Colly 中出现 “colly:invalid memory address or nil pointer dereference” 错误的原因是什么?
时间:2024-10-29 13:03:50 478浏览 收藏
大家好,我们又见面了啊~本文《Colly 中出现 “colly:invalid memory address or nil pointer dereference” 错误的原因是什么?》的内容中将会涉及到等等。如果你正在学习Golang相关知识,欢迎关注我,以后会给大家带来更多Golang相关文章,希望我们能一起进步!下面就开始本文的正式内容~

colly 中出现的 “colly:invalid memory address or nil pointer dereference”
在使用 colly 时,出现了 “colly:invalid memory address or nil pointer dereference” 错误信息,这通常指示存在空指针引用。根据提供的代码,错误可能源自以下位置:
package main
import (
"fmt"
"io"
"net/http"
"os"
"strings"
"github.com/gocolly/colly"
)
func main() {
// 设置 colly 实例
c := colly.newcollector()
// 在遇到图片时下载
c.onhtml("img[src]", downloadimages)
// 访问目标页面
c.visit("https://example.com")
}
func downloadimages(e *colly.htmlelement) {
srcref := desturl + e.attr("src")
res, _ := http.get(srcref)
if strings.contains(srcref, "http") && strings.contains(srcref, ".jpg") {
arr := strings.split(srcref, "/")
filename := strings.split(arr[len(arr)-1], ".")[0]
fmt.println("filename", filename)
f, _ := os.create("./images/" + filename + ".jpg")
defer f.close()
written, _ := io.copy(f, res.body)
fmt.println("info", written)
}
}错误分析
在 downloadimages 函数中,desturl 可能是一个未初始化的变量,导致在构建 srcref 时出现空指针引用。错误消息中提到的 “c:/users/dell/desktop/work/golang/colly/colly.go:55” 表示错误发生在上述代码的第 55 行,即 main 函数的 c.visit() 调用中。
修复方案
要解决此错误,请确保在使用 desturl 之前对其进行初始化。例如,可以在 main 函数中声明并初始化 desturl:
var destUrl = "https://example.com/images/"
理论要掌握,实操不能落!以上关于《Colly 中出现 “colly:invalid memory address or nil pointer dereference” 错误的原因是什么?》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!
-
860 收藏
-
843 收藏
-
826 收藏
-
809 收藏
-
792 收藏
-
Golang · Go教程 | 10小时前 | HTTP · Go教程 · 问题排查 · io.ReadAll · JSON绑定 · Go教程 Go HTTP请求体 io.ReadAll Request Body JSON绑定244 收藏
-
Golang · Go教程 | 11小时前 | 跨域 · cors · options · Go教程 · net/http · 跨域 Access-Control-Allow-Origin 预检请求 Options Go教程 Go CORS275 收藏
-
Golang · Go教程 | 12小时前 | WaitGroup · 并发编程 · Go教程 · Go 1.25 · Go并发 WaitGroup errgroup Go 1.25 sync.WaitGroup.Go172 收藏
-
238 收藏
-
217 收藏
-
367 收藏
-
Golang · Go教程 | 5天前 | channel · select · Context · Go教程 · 性能排查 · select channel context default time.Ticker Go教程 CPU飙高 for select459 收藏
-
Golang · Go教程 | 5天前 | map · 基准测试 · 性能优化 · Go教程 · 内存分配 · 内存分配 Go性能优化 benchmark Go教程 map预分配 make map benchmem395 收藏
-
Golang · Go教程 | 5天前 | defer · 单元测试 · testing · Go教程 · t.Cleanup · defer 单元测试 Testing 子测试 Go教程 T.Cleanup 测试资源清理418 收藏
-
Golang · Go教程 | 5天前 | defer · Go教程 · 文件句柄 · 资源释放 · 数据库rows · defer for循环 文件句柄 资源释放 close Go教程 rows.Close421 收藏
-
Golang · Go教程 | 5天前 | HTTP · 文件上传 · Go教程 · 资源预算 · multipart · 文件上传 临时文件 ParseMultipartForm multipart Go教程 MaxBytesReader 资源预算237 收藏
-
Golang · Go教程 | 6天前 | 中间件 · HTTP · recover · Go教程 · 日志排障 · recover panic 结构化日志 HTTP中间件 request_id Go教程 接口排障111 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 485次学习