Skip to content

Instantly share code, notes, and snippets.

@ebuckley
Last active August 29, 2015 14:03
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 ebuckley/fe86d02552fd487c0829 to your computer and use it in GitHub Desktop.
Save ebuckley/fe86d02552fd487c0829 to your computer and use it in GitHub Desktop.
Daily programmer
/**
* Daily programmer weekly challenge http://www.reddit.com/r/dailyprogrammer/comments/2a3pzl/weekly_1_handling_console_input/
*/
package main
import (
"bufio"
"log"
"os"
"strconv"
)
func readlinesfrom(fd *os.File) (words []string, err error) {
var wordcount int
bio := bufio.NewReader(fd)
line, hasMoreInLine, err := bio.ReadLine()
if hasMoreInLine {
log.Println("buffer out of space")
}
if err != nil {
log.Panic(err)
}
wordcount, err = strconv.Atoi(string(line))
if err != nil {
log.Panic(err)
}
words = make([]string, wordcount)
for i := 0; i < wordcount; i++ {
line, hasMoreInLine, err := bio.ReadLine()
if hasMoreInLine {
log.Println("buffer out of space")
}
if err != nil {
log.Panic(err)
}
words[i] = string(line)
}
return
}
func main() {
words, err := readlinesfrom(os.Stdin)
if err != nil {
log.Panic("couldn't readj lines")
}
log.Println(words)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment