- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Golang Program to check if a file is directory or a file
In Go, a directory is a special type of file that holds other files and directories. A directory is used to organize files on a file system and provides a structure to the file system. Go provides several functions in the os package to work with directories, including: os.mkdir, os.remove, os.open, etc.
Syntax
func Stat(name string) (fi FileInfo, err error)
The Stat() function is present in os package and is used to check the status of a particular directory. The function takes a string parameter name, which represents the path of the directory. It returns a FileInfo structure, fi, and an error, err.
Algorithm
Step 1 − First, we need to import the fmt and os packages.
Step 2 − Then start the main() function. Inside the main initialize a variable named file and store in it the extension where the file or directory is present.
Step 3 − Now, check that whether the path provided is of a file or directory by using IsDir() method. If the path is of a directory then print that it is a directory.
Step 4 − Otherwise, print that the path is of a file.
Example 1
In this example, we will use the isDir() function present in os package. the program will display the result as a directory or file depending on whether the path provided by the user leads to the file or not.
package main import ( "fmt" "os" ) func main() { // providing the path of the file file := "C:/Users/LENOVO/Desktop/go" info, err := os.Stat(file) if err != nil { fmt.Println(err) return } if info.IsDir() { fmt.Println("It is a directory") } else { fmt.Println("It is a file") } }
Output
It is a directory
Example 2
In this example, we will use Lstat() method present in os package in order to find that whether the file provided by the user is a file or a directory.
package main import ( "fmt" "os" ) func main() { // giving the extension of the file file := "C:/Users/LENOVO/Desktop/go/newFile.txt" fileInfo, err := os.Lstat(file) if err != nil { fmt.Println(err) return } mode := fileInfo.Mode() if mode.IsDir() { fmt.Println("It is a directory") } else if mode.IsRegular() { fmt.Println("It is a regular file") } else { fmt.Println("It is something else") } }
Output
It is a regular file
Example 3
In this example, we will use STAT() method present in OS package in order to find that whether the file provided by the user is a file or a directory.
package main import ( "fmt" "os" ) func main() { // ENTER THE EXTENSION OF THE FILE OR DIRECTORY file := "C:/Users/LENOVO/Desktop/go/new.go" fileInfo, err := os.Stat(file) if err != nil { fmt.Println(err) return } if fileInfo.Mode().IsDir() { fmt.Println("It is a directory") } else { fmt.Println("It is a file") } }
Output
It is a file
Example 4
In this example, we will use isRegular() method in order to find that whether the file provided by the user is a file or a directory.
package main import ( "fmt" "os" ) func main() { // Replace this with the path to your file file := "C:/Users/LENOVO/Desktop/go" fileInfo, err := os.Stat(file) if err != nil { fmt.Println(err) return } if fileInfo.Mode().IsRegular() { fmt.Println("It is a file") } else { fmt.Println("It is a directory") } }
Output
It is a directory
Conclusion
We have successfully compiled and executed a go language program to check if the given file is a file or directory along with examples. here we have used four different approaches to check the result. In the first and second program we have used isDir() and lstat() function while in the third and fourth program we are using stat() and IsRegular() function respectively.
- Related Articles
- Java Program to check if a file or directory is readable
- C# Program to check if a path is a directory or a file
- How to check if a file is a directory or a regular file in Python?
- Golang Program to check a specified file is exist or not
- Golang Program to Search for a file in a directory
- Java Program to rename a file or directory
- Golang Program to check a file has writable permission or not
- Golang Program to check a file has readable permission or not
- Golang Program to check a directory is exist or not
- How to check if a File Type Exists in a Directory?
- Check for file or directory in Java
- Java Program to delete file or directory
- How to check if a file exists in Golang?
- Check whether a file is a directory in Java
- Java Program to delete a file or directory when the program ends
