{"id":1176,"date":"2020-01-16T18:33:03","date_gmt":"2020-01-16T18:33:03","guid":{"rendered":"https:\/\/golangbyexamples.com\/?p=1176"},"modified":"2020-02-23T18:30:04","modified_gmt":"2020-02-23T18:30:04","slug":"touch-file-golang","status":"publish","type":"post","link":"https:\/\/golangbyexamples.com\/touch-file-golang\/","title":{"rendered":"Touch a file in Go (Golang)"},"content":{"rendered":"\n
Touching a file means<\/p>\n\n\n\n
package main\n\nimport (\n \"fmt\"\n \"log\"\n \"os\"\n \"time\"\n)\n\nfunc main() {\n fileName := \"temp.txt\"\n _, err := os.Stat(fileName)\n if os.IsNotExist(err) {\n file, err := os.Create(\"temp.txt\")\n if err != nil {\n log.Fatal(err)\n }\n defer file.Close()\n } else {\n currentTime := time.Now().Local()\n err = os.Chtimes(fileName, currentTime, currentTime)\n if err != nil {\n fmt.Println(err)\n }\n }\n}<\/code><\/pre>\n\n\n\nOutput:<\/strong><\/p>\n\n\n\nWhen running the first time it will create the file. After the first time, it will update the modified time of the file.<\/p>\n","protected":false},"excerpt":{"rendered":"
Touching a file means Create an empty file if the file doesn’t already exist If the file already exists then update the modified time of the file. Output: When running the first…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[161,38],"class_list":["post-1176","post","type-post","status-publish","format-standard","hentry","category-tech","tag-empty","tag-file"],"yoast_head":"\n
Touch a file in Go (Golang) - Welcome To Golang By Example<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n