-
Notifications
You must be signed in to change notification settings - Fork 0
/
files.go
42 lines (34 loc) · 767 Bytes
/
files.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
package main
import (
"fmt"
"io"
"os"
"strings"
)
func main() {
readFromFile()
}
func readFromFile() {
// .:.:.: IO Util makes it easy :.:.:.
// fileName := "data.txt"
// bytes, _ := ioutil.ReadFile(fileName)
// fmt.Println(string(bytes))
// .:.:.: Reading to a byte array :.:.:.
file, _ := os.Open("data.txt")
fileInfo, _ := file.Stat()
// With the [FileInfo.Size()] I create a byte array with this file's size
bytesData := make([]byte, fileInfo.Size())
length, _ := file.Read(bytesData)
fmt.Println(string(bytesData[:length]))
}
func reader() {
reader := strings.NewReader("Hello world")
bytes := make([]byte, 3)
for {
length, err := reader.Read(bytes)
fmt.Printf("byte = %s\n", bytes[:length])
if err == io.EOF {
break
}
}
}