zoukankan      html  css  js  c++  java
  • 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:projectsrcmain.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
    ", 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
     
  • 相关阅读:
    SQL查询
    SQL语句
    SQL语句
    查询设计分析
    数据库引擎调整顾问
    详解执行计划
    详解索引连接类型
    查询开销
    利用SQL Profiler处理开销较大的查询
    状压dp的题目列表 (一)
  • 原文地址:https://www.cnblogs.com/jiftle/p/14836989.html
Copyright © 2011-2022 走看看