github.com/searKing/golang/go@v1.2.117/encoding/ini/write.go (about) 1 // Copyright 2020 The searKing Author. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package ini 6 7 import ( 8 "bytes" 9 "fmt" 10 "io" 11 "os" 12 ) 13 14 func (c *Config) Write(w io.Writer) error { 15 var buf bytes.Buffer 16 17 for k, v := range c.Values { 18 buf.WriteString(fmt.Sprintf("%s = %s\n", k, v)) 19 } 20 21 _, err := w.Write(buf.Bytes()) 22 return err 23 } 24 25 func (c *Config) WriteFile(filePath string) error { 26 filePathBak := fmt.Sprintf("%s.bak.tmp", filePath) 27 28 fd, err := os.OpenFile(filePathBak, os.O_CREATE|os.O_WRONLY, os.ModePerm) 29 if err != nil { 30 return err 31 } 32 33 err = c.Write(fd) 34 fd.Close() 35 36 if err != nil { 37 return err 38 } 39 40 return os.Rename(filePathBak, filePath) 41 }