Golang filepath.Walk panic error on large directory


func GetFiles() {
    err := filepath.Walk("C:\\", walkpath)
    if err != nil {
        fmt.Printf(err.Error())
    }
}

Panic error:

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x40 pc=0x46d0d6]

goroutine 1 [running]:
files.walkpath(0xc0820a8f80, 0xf, 0x0, 0x0, 0x664270, 0xc082408840, 0x0, 0x0)
        C:/project/src/files/files.go:11 +0x66
path/filepath.walk(0x529140, 0x3, 0x6641e8, 0xc082012240, 0x560d50, 0x0, 0x0)
        c:/go/src/path/filepath/path.go:370 +0x41c
path/filepath.Walk(0x529140, 0x3, 0x560d50, 0x0, 0x0)
        c:/go/src/path/filepath/path.go:396 +0xe8
files.GetFiles()
        C:/project/src/files/files.go:22 +0xc9
main.main()
        c:/project/src/main.go:12 +0x49
exit status 2

c:\project>go build c:\project\src\main.go

https://gobyexample.com/panic

How do you handle errors which occur during runtime?

Thanks!

  •  
    Check out this article; blog.golang.org/defer-panic-and-recover there's a common panic/recover pattern that gets used. Every error is different though. What does yours mean? If you're trying to say open a directory that doesn't exist or you don't have permissions to, recovering will just get control of execution back into your code, it's not gonna solve the problem of the directory being absent so you'll have to make some decisions about what should happen upon failure. – evanmcdonnal Feb 18 '16 at 17:37

1 Answer

 

 
5
 

You're not checking the error, and trying to call a method on a nil os.FileInfo interface:

func walkpath(path string, f os.FileInfo, err error) error {
    if err != nil {
        fmt.Println(err)
    } else {    
        fmt.Printf("%s with %d bytes\n", path,f.Size())
    }
    return nil
}

If you want to handle a panic at runtime, you can use recover. This should however be a last resort for unexpected panics. A panic is usually because of a programming error, and is intended to crash the program.

  •  
    i encountered this. if err is not nil, then it is not safe to dereference f. invalid memory address or nil pointer dereference - usually means f would be nil. but in this case, it is because when err is not nil, f just happens to be garbage that you cannot dereference. ie: a pointer to something no longer valid, or a pointer with a strange value like "2" or "-5", etc. – Rob Dec 23 '16 at 2:36
 
 
原文:https://stackoverflow.com/questions/35488397/golang-filepath-walk-panic-error-on-large-directory
 
posted @   jiftle  阅读(217)  评论(0编辑  收藏  举报
编辑推荐:
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
阅读排行:
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
历史上的今天:
2020-06-01 ubuntu安装ipython
2020-06-01 python调用.net动态库
2017-06-01 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件(转)
2017-06-01 vim语法高亮插件编写
点击右上角即可分享
微信分享提示