10 examples of 'golang append to file' in Go

Every line of 'golang append to file' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Go code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
12func AppendString(newLine string, fileName string) {
13 file, openError := os.OpenFile(fileName, os.O_APPEND|os.O_WRONLY, 0600)
14 ErrorCheck(openError)
15 bytesWritten, writeError := file.Write([]byte(newLine + "\n"))
16 fmt.Println(bytesWritten)
17 ErrorCheck(writeError)
18}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

131func appendCommon() error {
132 file, err := os.Create("./controllers/common/common.go")
133 if err != nil {
134 return errors.New("WebGen: Failed to common controller: " + err.Error())
135 }
136 defer file.Close()
137
138 _, err = file.Write([]byte(commonFile))
139 if err != nil {
140 log.Println(err)
141 }
142
143 return nil
144}
31func appendFile(filename string, data string) error {
32 fp, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY, 0755)
33 if err != nil {
34 return err
35 }
36 defer fp.Close()
37
38 _, err = fp.WriteString(data)
39 if err != nil {
40 return err
41 }
42 return nil
43}
1589func appendToFile(filePath, stringToAppend string) error {
1590 f, err := os.OpenFile(filePath, os.O_APPEND|os.O_WRONLY, 0644)
1591 if err != nil {
1592 return err
1593 }
1594 defer f.Close()
1595
1596 _, err = f.WriteString(stringToAppend)
1597 return err
1598}
169func (f *File) AppendBytes(data []byte) {
170 file, err := os.OpenFile(f.filename, os.O_APPEND|os.O_WRONLY, 0644)
171 if err != nil {
172 f.err = err
173 return
174 }
175 defer file.Close()
176
177 if _, err := file.Write(data); err != nil {
178 f.err = err
179 return
180 }
181}
206func AppendFile(fileName string, content string) error {
207 bs, err := ReadAll(fileName)
208 if err != nil {
209 return errors.Wrapf(err, "read file %s failed", fileName)
210 }
211 if strings.Contains(string(bs), content) {
212 return nil
213 }
214 err = WriteFile(fileName, []byte(fmt.Sprintf("%s\n%s", bs, content)))
215 if err != nil {
216 return errors.Wrapf(err, "write file %s failed", fileName)
217 }
218 return nil
219}
86func AppendToFile(path string, values string) error {
87 EnsureDirectory(path)
88 if _, err := os.Stat(path); err != nil {
89 ioutil.WriteFile(path, []byte(""), os.ModePerm)
90 }
91 f, err := os.OpenFile(path, os.O_APPEND|os.O_WRONLY, 0666)
92 if err != nil {
93 return err
94 }
95 defer f.Close()
96 f.WriteString("\n")
97 _, err = f.WriteString(values)
98 if err != nil {
99 return err
100 }
101 return nil
102}
351func (cfg *Config) AddTextFile(filename, data string, mode uint) {
352 addFile(cfg.AddRunCmd, filename, []byte(data), mode, false)
353}
36func Append(path string, r io.Reader) error {
37 logger.Sugar.Debugf("append file: %s", path)
38
39 f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600)
40 if err != nil {
41 return err
42 }
43 defer f.Close()
44
45 bufSize := 1024
46 buf := make([]byte, bufSize)
47
48 for {
49 n, err := r.Read(buf)
50 if err == io.EOF {
51 break
52 }
53 nw, e := f.Write(buf[:n])
54 logger.Sugar.Debugf("Append: read %d bytes, write %d bytes, read err: %s, write err: %s", n, nw, err, e)
55 if e != nil {
56 return e
57 }
58 }
59
60 return nil
61}
121func (this *FsStore) LogSavedFile(path string, content string) {
122 util.FileAppendNewLine(path, content)
123}

Related snippets