File Operations with the Go Programming Language (Golang)

Talking about the file permission, as in Linux, O_CREATEif the file does not exist with parameter, you can assign the permission value to the file while creating a new file. Click for detailed information about permissions .

2.3- Printing the File Line by Line

file, err := os.OpenFile("deneme.txt", os.O_RDONLY, 0755)
if err != nil {
panic(err)
}
// Blok sonunda dosyayı kapat
defer file.Close()
// Satır satır oku
scanner := bufio.NewScanner(file)
for scanner.Scan() {
satir := scanner.Text()
fmt.Println(satir)
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}

2.4- Accessing File Meta Information

file, err := os.Stat("deneme.txt")
if err != nil {
panic(err)
}
fmt.Println("Dosya Adı: ", file.Name())
fmt.Println("Dosya Boyutu: ", file.Size())
fmt.Println("Dosya İzni: ", file.Mode())
fmt.Println("Dosya Tarihi: ", file.ModTime())
fmt.Println("Dosya Klasör mü: ", file.IsDir())
fmt.Println("Dosya Kaynağı: ", file.Sys())

If we look at the output of this program;

Dosya Adı:  deneme.txt
Dosya Boyutu: 140
Dosya İzni: -rw-rw-rw-
Dosya Tarihi: 2020-09-03 13:53:54.5489958 +0300 +03
Dosya Klasör mü: false
Dosya Kaynağı: &{32 {3989454219 30844384} {3079957368 30844484} {2218824230 30835168} 0 140}

Let’s collect the information we can get in a list;

  • Name() string // Shows the file name
  • Size() int64 // Shows the file size in bytes
  • Mode() FileMode // Shows the file type
  • ModTime() time.Time // Shows the last edit date of the file
  • IsDir() bool // Indicates whether the file is a folder
  • Sys() interface {} // Shows the file source

3- Writing to File Operations

  • O_RDONLY : read-onlyOpens the file for reading only ( ). (We used it in reading operations above)
  • O_WRONLY : write-onlyOpens the file for writing ( ) only .
  • O_RDWR : read-writeOpens the file for both reading and writing ( ).

One of these two parameters must be used. However, in addition to the above, there are five other variables that we can preferentially use;

  • O_APPEND : Adds the data to the end of the file while writing to the file.
  • O_CREATE : When opening the file, if there is no file with the specified name, it creates.
  • O_EXCL : Used with O_CREATE. A file with the specified name must not exist in order to create a new file.
  • O_SYNC : Allows the file to be opened for synchronous I / O.
  • O_TRUNC : If the file already exists and it is a normal file, truncate 0 length file.

So let’s do a little example.

file, err := os.OpenFile("deneme.txt", os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
if err != nil {
log.Fatal(err)
}
// Fonksiyon sonunda dosyayı kapat
defer file.Close()
// Dosyaya yaz
len, err := file.WriteString("Merhaba, bu string dosyanın sonuna eklenecek")
if err != nil {
log.Fatal(err)
} else {
log.Println("Yazılan byte boyutu: " + strconv.Itoa(len))
}

As you can see, it’s that simple to write to the file. If you want to go to the bottom line after the value you have written, "\n"add it to the end of your text .

4- Closing Files

file, err := os.Open("deneme.txt")
if err != nil {
log.Fatal(err)
}
file.Close()

5- Deleting Files

  • Remove() : Deletes the specified file
  • RemoveAll() : Deletes all files in a directory
err := os.Remove("deneme.txt") 
if err != nil {
log.Fatal(err)
}
err := os.RemoveAll("/home/user/test")
if err != nil {
log.Fatal(err)
}

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store