×

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

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

Parameters

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




Comments and Discussions!

Load comments ↻





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