ADVERTISEMENT
ADVERTISEMENT

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

os.Chown()

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.

Syntax:

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

Parameter(s):

  • 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.

Example:

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

package main

import (
	"fmt"
	"os"
)

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

Output:

File ownership changed successfully

Golang os Package »


ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.