英文:
Golang filepath.Walk panic error on large directory
问题
我正在尝试遍历C盘上的所有文件,但我无法弄清楚为什么在这样做时会出现恐慌错误,因为我告诉遍历函数返回一个空错误代码。
package files
import (
"path/filepath"
"os"
"fmt"
)
func walkpath(path string, f os.FileInfo, err error) error {
fmt.Printf("%s with %d bytes\n", path,f.Size())
return nil
}
func GetFiles() {
err := filepath.Walk("C:\\", walkpath)
if err != nil {
fmt.Printf(err.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
你如何处理运行时发生的错误?
谢谢!
英文:
I am trying to walk through all files on the C drive which I read can be inefficient in Go, but I can't work out why I keep getting a panic error when doing this as I tell the walk function to return a nil error code.
package files
import (
"path/filepath"
"os"
"fmt"
)
func walkpath(path string, f os.FileInfo, err error) error {
fmt.Printf("%s with %d bytes\n", path,f.Size())
return nil
}
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
How do you handle errors which occur during runtime?
Thanks!
答案1
得分: 6
你没有检查错误,并且试图在一个空的os.FileInfo
接口上调用一个方法:
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
}
如果你想在运行时处理panic
,你可以使用recover
。然而,这应该是处理意外panic
的最后手段。panic
通常是由于编程错误引起的,意在使程序崩溃。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论