package main
import (
"fmt"
"os"
)
func main() {
f, err := os.Open("/test.txt")
err, ok := err.(*os.PathError);
if ok {
fmt.Println("File at path", err.Path, "failed to open")
return
}
fmt.Println(f.Name(), "opened successfully")
}
这样写报错:# command-line-arguments
usercode/file.go:12: err.Path undefined (type error has no field or method Path)
把加粗的err换成别的,比如err2,可以正常输出
但是如下写法,没问题:
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")
}
--
FROM 120.242.252.*