From 869142a31428c6f656bd83ea9fc7a304f4423aec Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sun, 18 Nov 2012 13:47:26 -0500 Subject: [PATCH] Getting sidetracked writing utility functions --- algs4/src/meatballhat.com/algs4/io.go | 26 ++++++++++++++++++++++ algs4/src/meatballhat.com/algs4/io_test.go | 11 +++++++++ 2 files changed, 37 insertions(+) create mode 100644 algs4/src/meatballhat.com/algs4/io.go create mode 100644 algs4/src/meatballhat.com/algs4/io_test.go diff --git a/algs4/src/meatballhat.com/algs4/io.go b/algs4/src/meatballhat.com/algs4/io.go new file mode 100644 index 0000000..2b87eec --- /dev/null +++ b/algs4/src/meatballhat.com/algs4/io.go @@ -0,0 +1,26 @@ +package algs4 + +import ( + "bufio" + "io" + "strconv" +) + +func ReadInts(infile io.Reader) ([]int64, error) { + ints := make([]int64, 0) + + var i int64 + + fileReader := bufio.NewReader(infile) + line, err := fileReader.ReadString('\n') + for err == nil { + i, err = strconv.ParseInt(line, 10, 0) + if err != nil { + return nil, err + } + ints = append(ints, i) + line, err = fileReader.ReadString('\n') + } + + return ints, nil +} diff --git a/algs4/src/meatballhat.com/algs4/io_test.go b/algs4/src/meatballhat.com/algs4/io_test.go new file mode 100644 index 0000000..14dbb73 --- /dev/null +++ b/algs4/src/meatballhat.com/algs4/io_test.go @@ -0,0 +1,11 @@ +package algs4_test + +import ( + "testing" +) + +func TestReadInts(t *testing.T) { + if 1 == 0 { + t.Error("WAT") + } +}