Created
January 9, 2020 15:21
-
-
Save pmagwene/1eeeb2f47a9c2f01ef445df66443c5ca to your computer and use it in GitHub Desktop.
Go record parsing example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"fmt" | |
"io" | |
"log" | |
"os" | |
"strings" | |
"time" | |
) | |
type Record struct { | |
Words []string | |
} | |
func NewRecord() *Record { | |
return &Record{Words: make([]string, 0)} | |
} | |
func ParseRecord(s string) *Record { | |
// return NewRecord() | |
return &Record{Words: strings.Fields(s)} | |
} | |
type Table struct { | |
Records []*Record | |
} | |
func NewTable() *Table { | |
return &Table{Records: make([]*Record, 0)} | |
} | |
func ParseTable(r io.Reader) (*Table, error) { | |
tbl := NewTable() | |
//needed to deal with large lines | |
const maxCapacity = 512 * 1024 | |
buf := make([]byte, maxCapacity) | |
scanner := bufio.NewScanner(r) | |
scanner.Buffer(buf, maxCapacity) | |
for scanner.Scan() { | |
line := scanner.Text() | |
rec := ParseRecord(line) | |
tbl.Records = append(tbl.Records, rec) | |
} | |
err := scanner.Err() | |
return tbl, err | |
} | |
func main() { | |
var f io.Reader | |
if len(os.Args) < 2 { | |
log.Fatalln("Must specify file argument") | |
} | |
fname := os.Args[1] | |
f, err := os.Open(fname) | |
if err != nil { | |
log.Fatalln("problem opening file") | |
} | |
t0 := time.Now() | |
tbl, err := ParseTable(f) | |
if err != nil { | |
log.Panicln(err) | |
} | |
t1 := time.Now() | |
fmt.Printf("Time elapsed to parse records: %v\n", t1.Sub(t0)) | |
fmt.Printf("Number of records: %d \n", len(tbl.Records)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment