Created
March 30, 2020 09:12
-
-
Save d1y/cca5c8067cc7f645fb21e7204215ead8 to your computer and use it in GitHub Desktop.
go读取文件
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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