×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

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

Parameters

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




Comments and Discussions!

Load comments ↻





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