Standard library / os

os.File.Chown()

Changes ownership of an open file. -1 preserves the respective ID on Unix. Permission restrictions still apply. Example intended for Linux/AMD64 with Go 1.23.2 in a disposable working directory; other platforms may differ. Expected stdout: 'ok\n'.

Formal Syntax & Structure

go syntaxgo
func (f *File) Chown(uid, gid int) error

Parameters & Attributes

NameTypeDescription
uidintUser ID; -1 leaves it unchanged on Unix.
gidintGroup ID; -1 leaves it unchanged on Unix.

Return Value

error

Changes ownership of an open file.

Working Example

Example Snippetgo
package main
import (  "fmt"  "os")
func must(e error) {  if e != nil {    panic(e)  }}func temp() *os.File { f, e := os.CreateTemp(".", "fixture-"); must(e); return f }func main() {  f := temp()  defer os.Remove(f.Name())  defer f.Close()  must(f.Chown(-1, -1))  fmt.Println("ok")}
Go Reference