Skip to content

Instantly share code, notes, and snippets.

@pmagwene
Created January 9, 2020 15:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pmagwene/1eeeb2f47a9c2f01ef445df66443c5ca to your computer and use it in GitHub Desktop.
Save pmagwene/1eeeb2f47a9c2f01ef445df66443c5ca to your computer and use it in GitHub Desktop.
Go record parsing example
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