Last active
August 29, 2015 14:03
-
-
Save ebuckley/fe86d02552fd487c0829 to your computer and use it in GitHub Desktop.
Daily programmer
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
/** | |
* 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