- 使用go,读取指定文件,并输出到终端
package main import ( "bufio" "flag" "fmt" "io" "os" ) func main() { var path string flag.StringVar(&path, "f", "", "the file path") flag.Parse() fmt.Println(path) file, err := os.Open(path) if err != nil { fmt.Println("this is err ", err) } defer file.Close() reader := bufio.NewReader(file) for { str, _, err := reader.ReadLine() if err == io.EOF { break } fmt.Println(string(str)) } }
- 效果:
-