updated Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/updated/ Sun, 23 Feb 2020 18:47:27 +0000 en-US hourly 1 https://wordpress.org/?v=6.8.1 https://i0.wp.com/golangbyexamples.com/wp-content/uploads/2021/05/cropped-go_border-1.png?fit=32%2C32&ssl=1 updated Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/updated/ 32 32 159787465 Change the modified/updated time and access time of a file in Go (Golang) https://vikasboss.github.io/change-updated-time-file-go/ https://vikasboss.github.io/change-updated-time-file-go/#respond Sun, 23 Feb 2020 18:43:13 +0000 https://vikasboss.github.io/?p=1479 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. Code: Output:

The post Change the modified/updated time and access time of a file in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
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.

The post Change the modified/updated time and access time of a file in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/change-updated-time-file-go/feed/ 0 1479