packagemainimport"fmt"import"os"// open file. read first 200 bytes. print it as string
// getHeadBytes gets the first n bytes of a file
func getHeadBytes(path string, n int) []byte {
xfile, err := os.Open(path) // For read access.
if err != nil {
panic(err)
}
deferxfile.Close()
xheadBytes := make([]byte, n)
m, err := xfile.Read(xheadBytes)
if err != nil {
panic(err)
}
returnxheadBytes[:m]
}
funcmain() {
varxfilePath = "/Users/xah/web/xahlee_info/golang/golang_read_file.html"
fmt.Printf("%v\n", string(getHeadBytes(xfilePath, 200)))
}