Skip to content

Instantly share code, notes, and snippets.

@ceaksan
Last active January 5, 2018 10:44
Show Gist options
  • Save ceaksan/618b36dcd6041026b5706608ffa32abc to your computer and use it in GitHub Desktop.
Save ceaksan/618b36dcd6041026b5706608ffa32abc to your computer and use it in GitHub Desktop.
package main
import (
"bufio"
"fmt"
"io"
"os"
)
func main() {
// file name
fileName := "newFile.txt"
if _, err := os.Stat(fileName); os.IsNotExist(err) {
// create a file
_, err := os.Create(fileName)
checkError(err)
fmt.Println(fileName + " is created.")
} else {
// file exists
fmt.Println(fileName + " exists.")
}
// For read access.
fileData, err := os.OpenFile(fileName, os.O_WRONLY|os.O_APPEND, 0666)
checkError(err)
fileStat, err := os.Stat(fileName)
checkError(err)
if fileStat.Size() == 0 {
fmt.Println("It is fresh!")
}
reader := bufio.NewReader(os.Stdin)
fmt.Print("\nText: ")
content, _ := reader.ReadString('\n')
ln, err := io.WriteString(fileData, content)
checkError(err)
fmt.Printf("%v character is included.", ln-1)
fileData.Close()
}
func checkError(err error) {
if err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment