Forked from novalagung/golang-create-read-write-delete-file.go
Created
May 12, 2017 08:29
-
-
Save SeptiyanAndika/d19ab1c9d12cd37063e5a0732a5ad44e to your computer and use it in GitHub Desktop.
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 ( | |
"fmt" | |
"io" | |
"os" | |
) | |
var path = "/Users/novalagung/Documents/temp/test.txt" | |
func main() { | |
createFile() | |
writeFile() | |
readFile() | |
deleteFile() | |
} | |
func createFile() { | |
// detect if file exists | |
var _, err = os.Stat(path) | |
// create file if not exists | |
if os.IsNotExist(err) { | |
var file, err = os.Create(path) | |
checkError(err) | |
defer file.Close() | |
} | |
} | |
func writeFile() { | |
// open file using READ & WRITE permission | |
var file, err = os.OpenFile(path, os.O_RDWR, 0644) | |
checkError(err) | |
defer file.Close() | |
// write some text to file | |
_, err = file.WriteString("halo\n") | |
checkError(err) | |
_, err = file.WriteString("mari belajar golang\n") | |
checkError(err) | |
// save changes | |
err = file.Sync() | |
checkError(err) | |
} | |
func readFile() { | |
// re-open file | |
var file, err = os.OpenFile(path, os.O_RDWR, 0644) | |
checkError(err) | |
defer file.Close() | |
// read file | |
var text = make([]byte, 1024) | |
for { | |
n, err := file.Read(text) | |
if err != io.EOF { | |
checkError(err) | |
} | |
if n == 0 { | |
break | |
} | |
} | |
fmt.Println(string(text)) | |
checkError(err) | |
} | |
func deleteFile() { | |
// delete file | |
var err = os.Remove(path) | |
checkError(err) | |
} | |
func checkError(err error) { | |
if err != nil { | |
fmt.Println(err.Error()) | |
os.Exit(0) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment