Open In App

filepath.Ext() Function in Golang With Examples

Last Updated : 10 May, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

In Go language, path package used for paths separated by forwarding slashes, such as the paths in URLs. The filepath.Ext() function in Go language used to return the file name extension used by the specified path. The extension is the suffix beginning at the final dot in the final element of path; it is empty if there is no dot. Moreover, this function is defined under the path package. Here, you need to import the “path/filepath” package in order to use these functions.

Syntax:

func Ext(path string) string

Here, ‘path’ is the specified path.

Return Value: It returns the file name extension used by the specified path.

Example 1:




// Golang program to illustrate the usage of
// filepath.Ext() function
  
// Including the main package
package main
  
// Importing fmt and path/filepath
import (
    "fmt"
    "path/filepath"
)
  
// Calling main
func main() {
  
    // Calling the Ext() function
    fmt.Println(filepath.Ext("gfg.org"))
    fmt.Println(filepath.Ext("GeeksforGeeks."))
    fmt.Println(filepath.Ext(".org"))
      
}


Output:

.org
.
.org

Example 2:




// Golang program to illustrate the usage of
// filepath.Ext() function
  
// Including the main package
package main
  
// Importing fmt and path/filepath
import (
    "fmt"
    "path/filepath"
)
  
// Calling main
func main() {
  
    // Calling the Ext() function
    fmt.Println(filepath.Ext("a.b"))
    fmt.Println(filepath.Ext("gfg"))
    fmt.Println(filepath.Ext("ide.gfg.org"))
    fmt.Println(filepath.Ext(".ab.cd"))
      
}


Output:

.b

.org
.cd


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads