Welcome To Golang By Example

Touch a file in Go (Golang)

Touching a file means

package main

import (
    "fmt"
    "log"
    "os"
    "time"
)

func main() {
    fileName := "temp.txt"
    _, err := os.Stat(fileName)
    if os.IsNotExist(err) {
        file, err := os.Create("temp.txt")
        if err != nil {
            log.Fatal(err)
        }
        defer file.Close()
    } else {
        currentTime := time.Now().Local()
        err = os.Chtimes(fileName, currentTime, currentTime)
        if err != nil {
            fmt.Println(err)
        }
    }
}

Output:

When running the first time it will create the file. After the first time, it will update the modified time of the file.