Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Golang os.Chown() Function with Examples

Golang | os.Chown() Function: Here, we are going to learn about the Chown() function of the os package with its usages, syntax, and examples.
Submitted by IncludeHelp, on November 07, 2021


In the Go language, the os package provides a platform-independent interface to operating system (Unix-like) functionality. The Chown() function is an inbuilt function of the os package, it is used to change the numeric uid and gid of the named file. If the file is a symbolic link, it changes the uid and gid of the link's target. A uid or gid of -1 means to not change that value. If there is an error, it will be of type *PathError.

It accepts three parameters (name string, uid, gid int) and returns <nil> if there is no error; otherwise, it returns an error.


func Chown(name string, uid, gid int) error


  • name - File's name
  • uid - Numeric uid
  • gid - Numeric gid

Return Value

The return type of the os.Chown() function is an error, it returns <nil> if there is no error; otherwise, it returns an error.


// Golang program to demonstrate the
// example of Chown() function

package main

import (

func main() {
	err := os.Chown("Sample.txt", os.Getuid(), os.Getgid())
	if err != nil {
	} else {
		fmt.Println("File ownership changed successfully")


File ownership changed successfully

Golang os Package »

Comments and Discussions!

Load comments ↻

Copyright © 2024 www.includehelp.com. All rights reserved.