Oh, that's nice, didnt think the solution is that easy, thank you, that was what I was looking for!
2017 m. kovas 21 d., antradienis 09:45:13 UTC+2, Pierre Curto rašė: > > Hello, > > You keep appending json objects to your file, which produces invalid json > after the first item. > You either need to produce valid json, for instance by putting your items > into an array, or use a streaming json parser that parses items > individually. > I have changed your example to perform the last option and here it is: > https://play.golang.org/p/n6fWA8bTAb. > > Note that you should probably return errors instead of bailing out right > away, but it all depends on what you are trying to do. > > P. > > Le lundi 20 mars 2017 23:27:15 UTC+1, Paulius Daubaris a écrit : >> >> Hello, I am writing this application, where I can store some reminders >> for myself in CLI to not forget something important. >> >> My question is how can I save my input data or lets say what's the best >> way to do so? I've tried JSON, but it didnt work out, primarily because I >> cant really figure it out. Anyhow if JSON is the way to go, I've been stuck >> and it got me really frustrated. >> >> Example: >> >> The function which creates new reminder: >> >> func newReminder() { >> checkFile() //Simple check if file is not in the directory it creates >> one >> >> var text string >> date := time.Now() >> dateString := date.String() >> file, err := os.OpenFile("rem.json", os.O_RDWR|os.O_APPEND, >> os.ModePerm) >> >> if err != nil { >> log.Fatal(err) >> } >> >> fmt.Print("Description: ") >> >> scanner := bufio.NewScanner(os.Stdin) >> if ok := scanner.Scan(); ok { >> text = scanner.Text() >> } >> >> reminder := &Name{dateString[:19], text} >> newReminder, _ := json.Marshal(&reminder) >> >> file.Write(newReminder) >> file.Close() >> } >> >> Here everything is working fine. I execute it with ./main -n, write new >> reminder, display it and it goes well. Although when I add another reminder >> and try to display it i get an error: invalid character '{' after >> top-level value >> Its because when I cat out my json file it looks like this : >> >> {"Date":"2017-03-20 10:46:48","Description":"new"}{"Date":"2017-03-20 >> 10:46:51","Description":"new .go"} >> >> So basically it has no root node. So again, is there a way to wrap it up? If >> the code is confusing here's the whole. https://play.golang.org/p/d0mlZw3ZH- >> >> Thanks in advance. >> >> >> >> >> -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.