-
Notifications
You must be signed in to change notification settings - Fork 42
/
banner.go
70 lines (60 loc) · 1.3 KB
/
banner.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"fmt"
"unicode/utf8"
)
func main() {
banner("Go", 6)
banner("G☺", 6)
s := "G☺"
fmt.Println("len:", len(s))
// code point = rune ~= unicode character
for i, r := range s {
fmt.Println(i, r)
if i == 0 {
fmt.Printf("%c of type %T\n", r, r)
// rune (int32)
}
}
b := s[0]
fmt.Printf("%c of type %T\n", b, b)
// byte (uint8)
x, y := 1, "1"
fmt.Printf("x=%v, y=%v\n", x, y)
fmt.Printf("x=%#v, y=%#v\n", x, y) // Use #v in debug/log
fmt.Printf("%20s!\n", s)
fmt.Println("g", isPalindrome("g"))
fmt.Println("go", isPalindrome("go"))
fmt.Println("gog", isPalindrome("gog"))
fmt.Println("g☺g", isPalindrome("g☺g"))
}
// isPalindrome("g") -> true
// isPalindrome("go") -> false
// isPalindrome("gog") -> true
// isPalindrome("gogo") -> false
func isPalindrome(s string) bool {
rs := []rune(s) // get slice of runes out of s
for i := 0; i < len(rs)/2; i++ {
if rs[i] != rs[len(rs)-i-1] {
return false
}
}
return true
}
func banner(text string, width int) {
padding := (width - utf8.RuneCountInString(text)) / 2 // BUG: len is in bytes
// padding := (width - len(text)) / 2 // BUG: len is in bytes
for i := 0; i < padding; i++ {
fmt.Print(" ")
}
fmt.Println(text)
for i := 0; i < width; i++ {
fmt.Print("-")
}
fmt.Println()
}
/*
multi
line
comment
*/