×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Golang os.IsPermission() Function with Examples

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

os.IsPermission()

In the Go language, the os package provides a platform-independent interface to operating system (Unix-like) functionality. The IsPermission() function is an inbuilt function of the os package, it is used to check whether the given error is known to report that permission is denied. The IsPermission() function is satisfied by ErrPermission as well as some syscall errors. This function predates errors.Is.

It accepts one parameter (err error) and returns a boolean indicating whether the error is known to report that permission is denied.

Syntax

func IsPermission(err error) bool

Parameters

  • err - Error

Return Value

The return type of the os.IsPermission() function is a bool, it returns a boolean indicating whether the error is known to report that permission is denied.

Example

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

package main

import (
	"fmt"
	"os"
	"path"
)

func main() {
	dir := "test_dir"

	os.Mkdir(dir, os.ModeDir)

	fileName := path.Join(dir, "file.txt")

	_, err := os.Create(fileName)
	if err != nil {
		fmt.Printf("Error: %v\n", err)
		fmt.Println("os.IsPermission:",os.IsPermission(err))
	} else {
		fmt.Println("File created")
	}
}

Output:

Error: open test_dir/file.txt: permission denied
os.IsPermission: true

Golang os Package »




Comments and Discussions!

Load comments ↻





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