// WriteTasks writes or append the struct to the file
WriteTasks writes or append the struct to the file
// WriteTasks writes or append the struct to the file
func WriteTasks(path string, tasks []Task, overwrite bool) error {
	var (
		err error
		f   *os.File
	)
	if overwrite {
		f, err = os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
	} else {
		f, err = os.OpenFile(path, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0755)
	}
	if err != nil {
		return nil
	}
	sort.Slice(tasks, func(i, j int) bool {
		return tasks[i].Date < tasks[j].Date
	})
	datawriter := bufio.NewWriter(f)
	if len(tasks) > 0 {
		for _, task := range tasks {
			line := fmt.Sprintf("%s,%s,%s", task.Date, task.Name, task.Done)
			_, _ = datawriter.WriteString(line + "\n")
		}
	} else {
		_, _ = datawriter.WriteString("")
	}
	datawriter.Flush()
	if err := f.Close(); err != nil {
		return err
	}
	return nil
}
func WriteTasks ( path string , tasks [ ] Task , overwrite bool ) error { var ( err error f * os . File ) if overwrite { f , err = os . OpenFile ( path , os . O_RDWR | os . O_CREATE | os . O_TRUNC , 0755 ) } else { f , err = os . OpenFile ( path , os . O_APPEND | os . O_WRONLY | os . O_CREATE , 0755 ) } if err != nil { return nil } sort . Slice ( tasks , func ( i , j int ) bool { return tasks [ i ] . Date < tasks [ j ] . Date } ) datawriter := bufio . NewWriter ( f ) if len ( tasks ) > 0 { for _ , task := range tasks { line := fmt . Sprintf ( " " , task . Date , task . Name , task . Done ) _ , _ = datawriter . WriteString ( line + " \n " ) } } else { _ , _ = datawriter . WriteString ( " " ) } datawriter . Flush ( ) if err := f . Close ( ) ; err != nil { return err } return nil }
WriteTasks writes or append the struct to the file
WriteTasks writes or append the struct to the file
// overwrite // append // Sort tasks first // Write content // normal case, write tasks // wipe