-
Notifications
You must be signed in to change notification settings - Fork 4
/
25-tableprinter-write-to-file.go
63 lines (51 loc) · 1.25 KB
/
25-tableprinter-write-to-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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Seriál "Programovací jazyk Go"
// https://www.root.cz/serialy/programovaci-jazyk-go/
//
// Sedmdesátá část
// Vykreslení tabulek do terminálu v jazyce Go
// https://www.root.cz/clanky/vykresleni-tabulek-do-terminalu-v-jazyce-go/
//
// Repositář:
// https://github.com/tisnik/go-root/
//
// Seznam příkladů ze sedmdesáté části:
// https://github.com/tisnik/go-root/blob/master/article_70/README.md
package main
import (
"log"
"os"
"github.com/lensesio/tableprinter"
)
type Record struct {
N int `header:"n"`
F int64 `header:"n!"`
}
// Factorial computes factorial for given n that might be positive integer
func Factorial(n int64) int64 {
switch {
case n < 0:
return 1
case n == 0:
return 1
default:
return n * Factorial(n-1)
}
}
const MaxN = 17
func main() {
file, err := os.Create("table3.txt")
if err != nil {
log.Fatal(err)
}
defer file.Close()
data := make([]Record, 0)
for n := 0; n <= MaxN; n++ {
data = append(data, Record{n, Factorial(int64(n))})
}
table := tableprinter.New(file)
table.BorderTop, table.BorderBottom, table.BorderLeft, table.BorderRight = true, true, true, true
table.CenterSeparator = "│"
table.ColumnSeparator = "│"
table.RowSeparator = "─"
table.Print(data)
}