forked from dennyzhang/cheatsheet-golang-A4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_file.go
44 lines (40 loc) · 921 Bytes
/
example_file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//-------------------------------------------------------------------
// @copyright 2018 DennyZhang.com
// Licensed under MIT
// https://www.dennyzhang.com/wp-content/mit_license.txt
//
// File: example_file.go
// Author : Denny <https://www.dennyzhang.com/contact>
// Description : go run example_file.go
// Link: https://www.golang-book.com/books/intro/13
// --
// Created : <2018-04-07>
// Updated: Time-stamp: <2018-04-18 23:49:26>
//-------------------------------------------------------------------
package main
import (
"fmt"
"os"
)
func main() {
file, err := os.Open("test.txt")
if err != nil {
// handle the error here
return
}
defer file.Close()
// get the file size
stat, err := file.Stat()
if err != nil {
return
}
// read the file
bs := make([]byte, stat.Size())
_, err = file.Read(bs)
if err != nil {
return
}
str := string(bs)
fmt.Println(str)
}
// File: example_file.go ends