-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathbyte.go
53 lines (46 loc) · 993 Bytes
/
byte.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
52
53
//
// run with "go run byte.go"
//
package main
import (
"fmt"
"io"
"os"
"strings"
)
var byteLf = byte(10) // line feed
var str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\n"
func main() {
fmt.Println(" == by numeric value")
// Sequentially go from 32 to 128
for i := 32; i <= 128; i++ {
b := byte(i)
if b == byteLf {
fmt.Printf("byte: lf %v %b \n", b, b)
} else {
fmt.Printf("byte: %v %s %b\n", b, []byte{b}, b)
}
}
fmt.Println(" == by character")
// Read through the bytes in the string
// See strings.go for example of reading through the runes in a string
reader := strings.NewReader(str)
for {
b, err := reader.ReadByte()
if err != nil {
if err == io.EOF {
return
}
fmt.Println("error: ", err)
os.Exit(1)
}
if b == byteLf {
fmt.Printf("byte: lf %b %v\n", b, b)
} else {
fmt.Printf("byte: %s %b %v\n", []byte{b}, b, b)
}
}
//
// See strings.go for example of converting byte array to string and back
//
}