Go基础_1_50 error

Tutorial: Go初级 Category: Go Published: 2026-04-07 13:58:26 Views: 21 Likes: 0 Comments: 0
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(), "打开文件成功。。")

}