-
Notifications
You must be signed in to change notification settings - Fork 3
/
helper.go
49 lines (44 loc) · 859 Bytes
/
helper.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
package main
import (
"database/sql"
"log"
"strings"
)
func printColumns(rows *sql.Rows) {
col, err := rows.Columns()
onError(err)
var sb strings.Builder
sb.WriteString("|")
for _, s := range col {
sb.WriteString(s)
sb.WriteString("|")
}
log.Println(sb.String())
}
func printRows(rows *sql.Rows) {
cols, _ := rows.Columns()
for rows.Next() {
columns := make([]string, len(cols))
columnPointers := make([]interface{}, len(cols))
for i := range columns {
columnPointers[i] = &columns[i]
}
err := rows.Scan(columnPointers...)
if err != nil {
panic(err)
}
var sb strings.Builder
sb.WriteString("|")
for _, s := range columnPointers {
sb.WriteString(*s.(*string))
sb.WriteString("|")
}
log.Println(sb.String())
}
}
func onError(err error) {
if err != nil {
log.Printf("Error %s", err)
panic(err)
}
}