-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path06_reader_seeker_absolute.go
51 lines (42 loc) · 1.15 KB
/
06_reader_seeker_absolute.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
45
46
47
48
49
50
51
// Seriál "Programovací jazyk Go"
// https://www.root.cz/serialy/programovaci-jazyk-go/
//
// Dvacátá třetí část
// Pokročilejší použití vstupně-výstupních funkcí standardní knihovny jazyka Go
// https://www.root.cz/clanky/pokrocilejsi-pouziti-vstupne-vystupnich-funkci-standardni-knihovny-jazyka-go/
//
// Repositář:
// https://github.com/tisnik/go-root/
//
// Seznam demonstračních příkladů z dvacáté třetí části:
// https://github.com/tisnik/go-root/blob/master/article_23/README.md
//
// Demonstrační příklad číslo 6:
// Použití operace Seek pro posun počítaný od začátku souboru.
package main
import (
"fmt"
"io"
"strings"
)
const inputString = "*** Hello world! ***"
const bufferSize = 6
func main() {
reader := strings.NewReader(inputString)
buffer := make([]byte, bufferSize)
reader.Seek(4, io.SeekStart)
for {
read, err := reader.Read(buffer)
if read > 0 {
fmt.Printf("read %d bytes translated into '%s'\n", read, buffer[:read])
}
if err == io.EOF {
fmt.Println("reached end of file")
break
}
if err != nil {
fmt.Printf("other error %v\n", err)
break
}
}
}