Golang在大型目录上使用filepath.Walk时出现恐慌错误。

huangapple go评论69阅读模式
英文:

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

https://gobyexample.com/panic

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.

huangapple
  • 本文由 发表于 2016年2月19日 01:28:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/35488397.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定