Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
package main
import (
var path = "/Users/novalagung/Documents/temp/test.txt"
func main() {
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)
if isError(err) { return }
defer file.Close()
fmt.Println("==> done creating file", path)
func writeFile() {
// open file using READ & WRITE permission
var file, err = os.OpenFile(path, os.O_RDWR, 0644)
if isError(err) { return }
defer file.Close()
// write some text line-by-line to file
_, err = file.WriteString("halo\n")
if isError(err) { return }
_, err = file.WriteString("mari belajar golang\n")
if isError(err) { return }
// save changes
err = file.Sync()
if isError(err) { return }
fmt.Println("==> done writing to file")
func readFile() {
// re-open file
var file, err = os.OpenFile(path, os.O_RDWR, 0644)
if isError(err) { return }
defer file.Close()
// read file, line by line
var text = make([]byte, 1024)
for {
_, err = file.Read(text)
// break if finally arrived at end of file
if err == io.EOF {
// break if error occured
if err != nil && err != io.EOF {
fmt.Println("==> done reading from file")
func deleteFile() {
// delete file
var err = os.Remove(path)
if isError(err) { return }
fmt.Println("==> done deleting file")
func isError(err error) bool {
if err != nil {
return (err != nil)
Copy link

csrgxtu commented Apr 15, 2016

copy and use your code.

Copy link

updogliu commented Jun 19, 2016

Very useful to beginners like me.

Copy link

aayushKumarJarvis commented Jul 19, 2016


Copy link

milosgajdos commented Nov 17, 2016

This is a bit buggy. Your defer file.Close() call will never be called if os.Create fails because you are calling os.Exit and according to documentation exit doesn't call any deferred calls.

Copy link

svigne1 commented Nov 21, 2016

Thank you!

Copy link

SerkanSipahi commented Apr 1, 2017

@milosgajdos83 :)

Copy link

lukeramsden commented Apr 4, 2017

@milosgajdos83 Would that matter though? Because if the program is exiting, it doesn't need to run the deferred functions. Either way, he could've just used fmt.Fatal().

Copy link

yutongz commented May 8, 2017

Helpful for me

Copy link

joe94 commented May 30, 2017

Like some of you have already suggested, this approach has some bugs. Remember that "defers will not be run when using os.Exit" (
I fix that issue here:

Copy link

novalagung commented Jul 7, 2017

@milosgajdos83 @joe94 thanks for the responses. I just modified the file for better logging. also all os.Exit() statements are removed now

Copy link

edwinyoyada commented Aug 8, 2017


Copy link

LyleScott commented Sep 11, 2017

Hey, this came in handy. I'd rather read code than words explaining it. thanks!

Copy link

abousselmi commented Nov 6, 2017

thanks !

Copy link

anikaraj commented Dec 19, 2017

I am getting " undefine: isError"

Copy link

cys920622 commented Jan 29, 2018

Helped me, thanks.

Copy link

kwkrisna2 commented Jul 9, 2018

Suwun cak noval

Copy link

alexrios commented Aug 29, 2018

Thanks mate!

Copy link

dana321 commented Dec 5, 2018

Did you know that you can also use the ioutil functions?

import "io/ioutil"

// read file
bytes,err:= ioutil.ReadFile(filename)
if err!=nil{

//write file
err:=ioutil.WriteFile("myfile.txt",[]byte("write this to file"),0644)
if err!=nil{

Copy link

webninjastar commented Apr 5, 2019

I think it has an issue in createFile() function. Because I tested the code without test.txt file in the path but there isn't "test.txt" file after running the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment