- 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 get the file extension
In golang, we can use the path package and string package to get the extension of a particular file. Here, in his article, we will obtain the file extension using two methods. In the first method, we will use path package function path.Ext. In the second method, we will use strings package function strings.LastIndex.
Method 1: Using Path Package
In this method, we will use path.Ext from path package to get the file extension. This built-in function will take the file as an input whose extension is to be printed.
Syntax
path.Ext
The Go path/filepath package makes it easier to work with files. It offers a method for joining and dividing file paths, extracting the file name and extension, and carrying out a number of additional actions on file paths. The function filepath.Ext returns the file extension associated with the provided path. It returns the part of a file path that begins with the last ". " character. An empty string is returned if the path contains no "." characters.
Algorithm
Step 1 − Create a package main and declare fmt(format package), path package in the program where main produces executable codes and fmt helps in formatting input and output.
Step 2 − Create a main function and in that function create a variable myfile and assign the required file to this variable.
Step 3 − Use path.Ext function to find the extension of required and file and obtain the extension in the extension variable.
Step 4 − Print the extension on the console.
Step 5 − The print statement is executed using fmt.Println() function from fmt package where ln means new line.
Example
In this example, we will use path.Ext function from path package to find file extension.
package main import ( "fmt" "path" ) func main() { myfile := "file1.txt"//create file extension := path.Ext(myfile) //obtain the extension of file fmt.Println("The extension of", myfile, "is", extension) //print extension }
Output
The extension of file1.txt is .txt
Method 2: Using Strings Package
In this illustration strings.LastIndex is used to find the last occurrence of dot(.) in the file name. The function returns -1, indicating that the file has no extension, if the dot cannot be located. If the dot is located, the file path substring from the dot to the end of the string is set to the ext variable. The remaining code is identical to that in the first example. Let’s see how its executed via code and algorithm.
Syntax
strings.LastIndex()
The strings package in Go offers a number of functions for handling strings. To locate the final instance of a specific substring within a string, use the LastIndex function found in the strings package.
Algorithm
Step 1 − Create a package main and declare fmt(format package), strings package in the program where main produces executable codes and fmt helps in formatting input and output.
Step 2 − Create a main function and in that function create a file and assign it to a variable named myfile.
Step 3 − Now, use strings.LastIndex function to get the last occurrence of the dot(.).
Step 4 − If no extension is obtained print the failure message.
Step 5 − But if the extension is obtained, receive it inside the ext variable, which is the substring of the file string starting from the index till the end.
Step 6 − Print the ext on the console using fmt.Println() function from fmt package where ln means new line.
Example
In this example, we will use strings.LastIndex from strings package to execute the program.
package main import ( "fmt" "strings" ) func main() { myfile := "file1.txt" extension := strings.LastIndex(myfile, ".") //obtain the extension after the dot if extension == -1 { fmt.Println("The file has no extension") return //if no extension is present print failure } ext := myfile[extension:] //obtain the extension in ext variable fmt.Println("The extension of", myfile, "is", ext) //print the extension of file }
Output
The extension of file1.txt is .txt
Conclusion
We executed the program of obtaining the file extension using two methods. In the first method, we used path package functions and in the second method, we used strings package functions.
- Related Articles
- C# program to get the extension of a file in C#
- How to get the file extension using PowerShell?
- Get file extension name in Java
- How to get file extension of file as a result of MySQL query?
- Golang program to get the name of the file from the absolute path
- Perl File Extension
- Get only the file extension from a column with file names as strings in MySQL?
- Golang program to delete a file
- Golang program to rename a file
- Golang program to copy one file into another file
- How to change file extension in Python?
- How to extract file extension using Python?
- Golang program to get the current weekday
- Golang program to create a new file
- Golang program to create a temporary file
