ADVERTISEMENT
ADVERTISEMENT

Golang os.DirFS() Function with Examples

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

os.DirFS()

In the Go language, the os package provides a platform-independent interface to operating system (Unix-like) functionality. The DirFS() function is an inbuilt function of the os package, it is used to get the file system (an fs.FS) for the tree of files rooted at the directory dir.

Note: DirFS("/prefix") only guarantees that the Open calls it makes to the operating system will begin with "/prefix": DirFS("/prefix").Open("file") is the same as os.Open("/prefix/file"). So if /prefix/file is a symbolic link pointing outside the /prefix tree, then using DirFS does not stop the access any more than using os.Open does. DirFS is therefore not a general substitute for a chroot-style security mechanism when the directory tree contains arbitrary content.

It accepts one parameter (dir string) and returns the file system (an fs.FS) for the tree of files rooted at the directory dir.

Reference: Clearenv

Syntax:

func DirFS(dir string) fs.FS

Parameter(s):

  • dir - Directory

Return Value:

The return type of the os.DirFS() function is fs.FS, it returns the file system (an fs.FS) for the tree of files rooted at the directory dir.

Example:

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

package main

import (
	"fmt"
	"os"
)

func main() {
	x := os.DirFS("/")

	// Printing the type & value of x
	fmt.Printf("x: %T, %v\n", x, x)
}

Output:

x: os.dirFS, /

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.