通过类型断言获取error类型,获得更详细的信息

package main

import (
    "fmt"
    "os"
)

func main() {
    f, err := os.Open("/test.txt")
    if err, ok := err.(*os.PathError); ok {
        fmt.Println("File at path", err.Path, "failed to open")
        return
    }
    fmt.Println(f.Name(), "opened successfully")
}




posted @ 2019-12-19 19:48  离地最远的星  阅读(214)  评论(0编辑  收藏  举报