/
24-tableprinter-factorial.go
55 lines (45 loc) · 1.15 KB
/
24-tableprinter-factorial.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
// 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 (
"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() {
data := make([]Record, 0)
for n := 0; n <= MaxN; n++ {
data = append(data, Record{n, Factorial(int64(n))})
}
table := tableprinter.New(os.Stdout)
table.BorderTop, table.BorderBottom, table.BorderLeft, table.BorderRight = true, true, true, true
table.CenterSeparator = "│"
table.ColumnSeparator = "│"
table.RowSeparator = "─"
table.Print(data)
}