Welcome To Golang By Example

Menu
  • Home
  • Blog
Menu

Change the modified/updated time and access time of a file in Go (Golang)

Posted on February 23, 2023February 23, 2023 by admin

os.Chtimes() function can be used to change the mtime(modified time) or atime(access time) of a file in Golang. Below is the signature of the function.

func Chtimes(name string, atime time.Time, mtime time.Time)

Code:

package main
import (
"fmt"
"os"
"time"
)
func main() {
fileName := "sample.txt"
currentTime := time.Now().Local()
//Set both access time and modified time of the file to the current time
err := os.Chtimes(fileName, currentTime, currentTime)
if err != nil {
fmt.Println(err)
}
}

Output:

Changes the atime and mtime of the file.
  • updated
  • Popular Articles

    Golang Comprehensive Tutorial Series

    All Design Patterns in Go (Golang)

    Slice in golang

    Variables in Go (Golang) – Complete Guide

    OOP: Inheritance in GOLANG complete guide

    Using Context Package in GO (Golang) – Complete Guide

    All data types in Golang with examples

    Understanding time and date in Go (Golang) – Complete Guide

    ©2023 Welcome To Golang By Example | Design: Web XP