Skip to content

Instantly share code, notes, and snippets.

@jadeallenx
Created August 20, 2012 04:56
Show Gist options
  • Save jadeallenx/3401076 to your computer and use it in GitHub Desktop.
Save jadeallenx/3401076 to your computer and use it in GitHub Desktop.
Solution for #84 learngo
package main
import (
"fmt"
"io/ioutil"
"strings"
"log"
"flag"
)
func main() {
flag.Parse()
var new_task string
if len(flag.Args()) != 0 {
new_task = strings.Join(flag.Args(), " ")
}
in, err := ioutil.ReadFile("todo.sample")
if err != nil {
log.Fatal("Couldn't open todo.sample", err)
}
line := strings.Split(string(in), "\n")
if new_task != "" {
line = append(line, new_task)
}
for i := 0; i < len(line); i++ {
fmt.Printf("%d %s\n", i, line[i])
}
err = ioutil.WriteFile("todo.sample", []byte(strings.Join(line, "\n")), 0644)
if err != nil {
log.Fatal("Couldn't write todo.sample", err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment