-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample_test.go
43 lines (36 loc) · 1.09 KB
/
example_test.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
package row_test
import (
"github.com/olekukonko/tablewriter"
"github.com/telyn/row"
"os"
)
// Country represents a country
type Country struct {
Name string
Population int `row.thousands:","`
HDI float32 `row.precision:"2"`
Cities []string
}
// NumCities could also have the signature 'func (c Country) NumCities() (int, error)
func (c Country) NumCities() int {
return len(c.Cities)
}
// TODO include output
func ExampleCountryTable() {
fields := []string{"Name", "Population", "NumCities"}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader(fields)
country := Country{"Argentine Republic", 4341700, 0.836, []string{"Buenos Aires", "Córdoba", "Rosario", "Mendoza", "La Plata"}}
values, err := row.From(country, fields)
if err != nil {
panic(err)
}
table.Append(values)
table.Render()
// Output:
// +--------------------+------------+-----------+
// | NAME | POPULATION | NUMCITIES |
// +--------------------+------------+-----------+
// | Argentine Republic | 4341700 | 5 |
// +--------------------+------------+-----------+
}