Created
August 20, 2012 04:56
-
-
Save jadeallenx/3401076 to your computer and use it in GitHub Desktop.
Solution for #84 learngo
This file contains hidden or 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
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