Go基础_1_50 error
package main
import (
"fmt"
"os"
)
func main() {
f, err := os.Open("test.txt")
if err != nil {
//log.Fatal(err)
fmt.Println(err) //open test.txt: no such file or directory
if ins, ok := err.(*os.PathError); ok {
fmt.Println(ins)
fmt.Println("1.Op:", ins.Op) // 1.Op: open
fmt.Println("2.Path:", ins.Path) // 2.Path: test.txt
fmt.Println("3.Err:", ins.Err) // 3.Err: The system cannot find the file specified.
}
return
}
fmt.Println(f.Name(), "打开文件成功。。")
}