一、按行读取

package  main

import (
"bufio"
"fmt"
"os"
)

func main() {
f,err := os.Open("./test.txt")
if err != nil {
fmt.Println(err.Error())
}
r := bufio.NewReader(f)
for {
s,err := r.ReadString('\n')
fmt.Println(s)
if err != nil {
fmt.Println(err.Error())
break
}
}

}

二、按字节读取

package  main

import (
"os"
)

func main() {
buf := make([]byte,10)
f,_ := os.Open("./test.txt")
defer f.Close()
for {
n,_ := f.Read(buf)
if n == 0 {
break
}
os.Stdout.Write(buf[:n])
}

}

三、读取全部内容

package main

import (
"fmt"
"io/ioutil"
"os"
)

func main() {
f, _ := os.OpenFile("hhh.sh", os.O_RDONLY,0600)
defer f.Close()
if err !=nil {
fmt.Println(err.Error())
} else {
contentByte,_:=ioutil.ReadAll(f)
fmt.Println(string(contentByte))
}
}