Standard library / os

os.Chtimes()

Updates access and modification timestamps; filesystem precision and permissions can affect results. Example uses a disposable temporary workspace. Expected stdout: '1700000000\n'.

Formal Syntax & Structure

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

Parameters & Attributes

NameTypeDescription
namestringName or pathname of the target.
atimetime.TimeRequested access time.
mtimetime.TimeRequested modification time.

Return Value

error

Updates access and modification timestamps; filesystem precision and permissions can affect results.

Working Example

Example Snippetgo
package main
import (  "fmt"  "os"  "time")
func check(e error) {  if e != nil {    panic(e)  }}func seed() { check(os.WriteFile("a.txt", []byte("abc"), 0600)) }func workspace() func() {  root, e := os.MkdirTemp("", "bytemind-go-")  check(e)  old, e := os.Getwd()  check(e)  check(os.Chdir(root))  return func() { check(os.Chdir(old)); check(os.RemoveAll(root)) }}func main() {  defer workspace()()  seed()  t := time.Unix(1700000000, 0)  check(os.Chtimes("a.txt", t, t))  i, e := os.Stat("a.txt")  check(e)  fmt.Println(i.ModTime().Unix())}
Go Reference