-
Notifications
You must be signed in to change notification settings - Fork 9
/
search.go
77 lines (65 loc) · 1.65 KB
/
search.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
71
72
73
74
75
76
77
package main
import (
"fmt"
"io"
"os"
"github.com/Valentin-Kaiser/go-dbase/dbase"
)
func main() {
// Open debug log file so we see what's going on
f, err := os.OpenFile("debug.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
fmt.Println(err)
return
}
dbase.Debug(true, io.MultiWriter(os.Stdout, f))
// Open the example database table.
table, err := dbase.OpenTable(&dbase.Config{
Filename: "../test_data/table/TEST.DBF",
})
if err != nil {
panic(err)
}
defer table.Close()
fmt.Printf(
"Last modified: %v Columns count: %v Record count: %v File size: %v \n",
table.Header().Modified(0),
table.Header().ColumnsCount(),
table.Header().RecordsCount(),
table.Header().FileSize(),
)
// Init the field we want to search for.
// Search for a product containing the word "test" in the name.
field, err := table.NewFieldByName("PRODNAME", "TEST")
if err != nil {
panic(err)
}
// Execute the search with an exact match.
records, err := table.Search(field, false)
if err != nil {
panic(err)
}
// Print all found records.
fmt.Println("Found records with match:")
for _, record := range records {
field = record.FieldByName("PRODNAME")
if field == nil {
panic("Field 'PRODNAME' not found")
}
fmt.Printf("%v \n", field.GetValue())
}
// Execute the search without exact match.
records, err = table.Search(field, true)
if err != nil {
panic(err)
}
// Print all found records.
fmt.Println("Found records with exact match:")
for _, record := range records {
field = record.FieldByName("PRODNAME")
if field == nil {
panic("Field 'PRODNAME' not found")
}
fmt.Printf("%v \n", field.GetValue())
}
}