Created
January 14, 2017 17:50
-
-
Save toruuetani/f6aa4751a66ef65646c1a4934471396b to your computer and use it in GitHub Desktop.
Prepend content to file by golang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"fmt" | |
"log" | |
"os" | |
) | |
func main() { | |
for i := 0; i < 10; i++ { | |
err := NewRecord("test.txt").Prepend(fmt.Sprintf("%02d", i)) | |
if err != nil { | |
log.Fatalf("failed to prepend: %+v", err) | |
} | |
} | |
} | |
type Record struct { | |
Filename string | |
Contents []string | |
} | |
func NewRecord(filename string) *Record { | |
return &Record{ | |
Filename: filename, | |
Contents: make([]string, 0), | |
} | |
} | |
func (r *Record) readLines() error { | |
if _, err := os.Stat(r.Filename); err != nil { | |
return nil | |
} | |
f, err := os.OpenFile(r.Filename, os.O_RDONLY, 0600) | |
if err != nil { | |
return err | |
} | |
defer f.Close() | |
scanner := bufio.NewScanner(f) | |
for scanner.Scan() { | |
if tmp := scanner.Text(); len(tmp) != 0 { | |
r.Contents = append(r.Contents, tmp) | |
} | |
} | |
return nil | |
} | |
func (r *Record) Prepend(content string) error { | |
err := r.readLines() | |
if err != nil { | |
return err | |
} | |
f, err := os.OpenFile(r.Filename, os.O_CREATE|os.O_WRONLY, 0600) | |
if err != nil { | |
return err | |
} | |
defer f.Close() | |
writer := bufio.NewWriter(f) | |
writer.WriteString(fmt.Sprintf("%s\n", content)) | |
for _, line := range r.Contents { | |
_, err := writer.WriteString(fmt.Sprintf("%s\n", line)) | |
if err != nil { | |
return err | |
} | |
} | |
if err := writer.Flush(); err != nil { | |
return err | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment