Home »
Golang »
Golang Reference
Golang os.Chtimes() Function with Examples
Golang | os.Chtimes() Function: Here, we are going to learn about the Chtimes() function of the os package with its usages, syntax, and examples.
Submitted by IncludeHelp, on November 14, 2021
os.Chtimes()
In the Go language, the os package provides a platform-independent interface to operating system (Unix-like) functionality. The Chtimes() function is an inbuilt function of the os package, it is used to change the access and modification times of the named file, similar to the Unix utime() or utimes() functions.
It accepts three parameters (name string, atime time.Time, mtime time.Time) and returns <nil> if there is no error; otherwise, it returns an error.
Syntax
func Chtimes(name string, atime time.Time, mtime time.Time) error
Parameters
- name - File's name
- atime - Access time
- mtime - Modified time
Return Value
The return type of the os.Chtimes() function is an error, it returns <nil> if there is no error; otherwise, it returns an error.
Example
// Golang program to demonstrate the
// example of Chtimes() function
package main
import (
"fmt"
"os"
"time"
)
func main() {
// Defining file's name
file := "sample.txt"
// Getting current time
curtime := time.Now().Local()
// Setting the both access time and
// modified time of the file
// of the current time
err := os.Chtimes(file, curtime, curtime)
if err != nil {
fmt.Println(err)
} else {
fmt.Println("Access & modified times changed.")
}
}
Output:
Access & modified times changed.
Golang os Package »