Beispiel #1
0
func main() {
	f, err := atomicwriter.NewWriter("file.txt")
	if err != nil {
		log.Panic(err)
	}
	defer f.Close()

	f.Write([]byte("Hello"))
	f.Write([]byte("world"))
	f.Write([]byte("\n"))
}
Beispiel #2
0
func (kv *kvFile) save() (err error) {
	f, err := atomicwriter.NewWriter(kv.path)
	if err != nil {
		return err
	}
	defer func() {
		err = f.Close()
	}()
	w := csv.NewWriter(f)
	defer w.Flush()

	kv.kvMem.Lock()
	defer kv.kvMem.Unlock()
	for k, v := range kv.kvMem.m {
		if err := w.Write([]string{k, string(v)}); err != nil {
			return err
		}
	}
	return nil
}