Standard library / os
os.Chmod()
Changes permission and supported mode bits of a named file; semantics vary across operating systems. Example uses a disposable temporary workspace. Expected stdout: '600\n'.
Formal Syntax & Structure
go syntaxgo
func Chmod(name string, mode FileMode) errorParameters & Attributes
| Name | Type | Description |
|---|---|---|
| name | string | Name or pathname of the target. |
| mode | FileMode | Requested file mode and permissions. |
Return Value
error
Changes permission and supported mode bits of a named file; semantics vary across operating systems.
Working Example
Example Snippetgo
package main
import ( "fmt" "os")
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() check(os.Chmod("a.txt", 0600)) i, e := os.Stat("a.txt") check(e) fmt.Printf("%o\n", i.Mode().Perm())}