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.

Updated on: 22-Feb-2023

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements