Golang Changing permissions, ownership, and timestamps
Example
package main
import (
"log"
"os"
"time"
)
func main() {
// Test File existence.
_, err := os.Stat("test.txt")
if err != nil {
if os.IsNotExist(err) {
log.Fatal("File does not exist.")
}
}
log.Println("File exist.")
// Change permissions Linux.
err = os.Chmod("test.txt", 0777)
if err != nil {
log.Println(err)
}
// Change file ownership.
err = os.Chown("test.txt", os.Getuid(), os.Getgid())
if err != nil {
log.Println(err)
}
// Change file timestamps.
addOneDayFromNow := time.Now().Add(24 * time.Hour)
lastAccessTime := addOneDayFromNow
lastModifyTime := addOneDayFromNow
err = os.Chtimes("test.txt", lastAccessTime, lastModifyTime)
if err != nil {
log.Println(err)
}
}
Most Helpful This Week
Go program to read list of files inside Zip file
Go program to compress list of files into Zip file
Go program to add or append content at the end of text file
Golang program to delete a specific file
Go program to read a text file character by character
Go program to Create directory or folder if not exist