Skip to content

Instantly share code, notes, and snippets.

@d1y
Created March 30, 2020 09:12
Show Gist options
  • Select an option

  • Save d1y/cca5c8067cc7f645fb21e7204215ead8 to your computer and use it in GitHub Desktop.

Select an option

Save d1y/cca5c8067cc7f645fb21e7204215ead8 to your computer and use it in GitHub Desktop.
go读取文件
package main
import (
"fmt"
"os"
)
func main() {
file, err := os.Open("filetoread.txt")
if err != nil {
fmt.Println(err)
return
}
defer file.Close()
fileinfo, err := file.Stat()
if err != nil {
fmt.Println(err)
return
}
filesize := fileinfo.Size()
buffer := make([]byte, filesize)
bytesread, err := file.Read(buffer)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("bytes read: ", bytesread)
fmt.Println("bytestream to string: ", string(buffer))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment