ADVERTISEMENT
ADVERTISEMENT

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

Parameter(s):

  • 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 »


ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.