-
-
Save novalagung/13c5c8f4d30e0c4bff27 to your computer and use it in GitHub Desktop.
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) | |
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 | |
} | |
// break if error occured | |
if err != nil && err != io.EOF { | |
isError(err) | |
break | |
} | |
} | |
fmt.Println("==> done reading from file") | |
fmt.Println(string(text)) | |
} | |
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 { | |
fmt.Println(err.Error()) | |
} | |
return (err != nil) | |
} |
Thank you!
@milosgajdos83 :)
@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().
Helpful for me
Like some of you have already suggested, this approach has some bugs. Remember that "defers will not be run when using os.Exit" (https://gobyexample.com/exit).
I fix that issue here: https://gist.github.com/joe94/9725e84392654fbb68ff16053ea6d5bf
@milosgajdos83 @joe94 thanks for the responses. I just modified the file for better logging. also all os.Exit()
statements are removed now
cool!
Hey, this came in handy. I'd rather read code than words explaining it. thanks!
thanks !
I am getting " undefine: isError"
Helped me, thanks.
Suwun cak noval
Thanks mate!
Did you know that you can also use the ioutil functions?
import "io/ioutil"
// read file
bytes,err:= ioutil.ReadFile(filename)
if err!=nil{
panic(err)
}
mystringdata:=string(bytes[:])
//write file
err:=ioutil.WriteFile("myfile.txt",[]byte("write this to file"),0644)
if err!=nil{
panic(err)
}
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.
This is a bit buggy. Your
defer file.Close()
call will never be called ifos.Create
fails because you are callingos.Exit
and according to documentation exit doesn't call any deferred calls.