{"id":2031,"date":"2020-04-17T17:41:09","date_gmt":"2020-04-17T17:41:09","guid":{"rendered":"https:\/\/golangbyexamples.com\/?p=2031"},"modified":"2020-04-17T17:41:21","modified_gmt":"2020-04-17T17:41:21","slug":"change-file-permissions-golang","status":"publish","type":"post","link":"https:\/\/golangbyexamples.com\/change-file-permissions-golang\/","title":{"rendered":"Change file permissions in Go (Golang)"},"content":{"rendered":"\n
os.Chmod() function can be used to change the permissions of an existing file. Below is the signature of the function<\/p>\n\n\n\n
func Chmod(name string, mode FileMode) error<\/code><\/pre>\n\n\n\nCode<\/strong><\/p>\n\n\n\npackage main\n\nimport (\n \"fmt\"\n \"log\"\n \"os\"\n)\n\nfunc main() {\n \/\/ Create new file\n new, err := os.Create(\"new.txt\")\n if err != nil {\n log.Fatal(err)\n }\n defer new.Close()\n\n stats, err := os.Stat(\"new.txt\")\n if err != nil {\n log.Fatal(err)\n }\n fmt.Printf(\"Permission File Before: %s\\n\", stats.Mode())\n err = os.Chmod(\"new.txt\", 0700)\n if err != nil {\n log.Fatal(err)\n }\n\n stats, err = os.Stat(\"new.txt\")\n if err != nil {\n log.Fatal(err)\n }\n fmt.Printf(\"Permission File After: %s\\n\", stats.Mode())\n}<\/code><\/pre>\n\n\n\nOutput:<\/strong><\/p>\n\n\n\nPermission File Before: -rw-r--r--\nPermission File After: -rwx------<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"os.Chmod() function can be used to change the permissions of an existing file. Below is the signature of the function Code Output:<\/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":[38,3,4,280],"class_list":["post-2031","post","type-post","status-publish","format-standard","hentry","category-tech","tag-file","tag-go","tag-golang","tag-permissions"],"yoast_head":"\n
Change file permissions 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