Instantly share code, notes, and snippets.

Embed
What would you like to do?
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