Sling Academy
Home/Golang/Go: How to get filename & extension from a path

Go: How to get filename & extension from a path

Last updated: November 26, 2024

Working with file paths in Go often involves extracting the filename and its extension from a full file path. The Go path/filepath package provides utilities for manipulating file paths across different operating systems. In this article, we'll cover how to extract the filename and file extension using Go.

Using the filepath Package

The filepath package offers functionality to work with file path manipulations. Its functions are file system agnostic, which makes managing paths across different platforms easier. We'll use the Base function to get the filename, and the Ext function to get the file extension.

Getting the Filename

To extract the filename from a file path, you can use the filepath.Base function, which returns the last element of the path:

package main

import (
    "fmt"
    "path/filepath"
)

func main() {
    path := "/your/directory/image.png"
    filename := filepath.Base(path)
    fmt.Println("Filename:", filename) // Output: "image.png"
}

In the code above, the filepath.Base function retrieves "image.png" from the given path.

Extracting the File Extension

Once you have the filename, you may want to retrieve just the file extension. Go's filepath.Ext function does this job. Here's how:

package main

import (
    "fmt"
    "path/filepath"
)

func main() {
    path := "/your/directory/image.png"
    extension := filepath.Ext(path)
    fmt.Println("File extension:", extension) // Output: ".png"
}

The filepath.Ext function extracts the extension from the filename "image.png", which results in ".png".

Combining Filename and Extension Extraction

You can combine both functions to neatly extract and separate the filename and its extension.

package main

import (
    "fmt"
    "path/filepath"
)

func main() {
    path := "/your/directory/image.png"
    filename := filepath.Base(path)
    extension := filepath.Ext(path)
    name := filename[:len(filename)-len(extension)]

    fmt.Println("Full Filename:", filename)    // Output: "image.png"
    fmt.Println("File Extension:", extension) // Output: ".png"
    fmt.Println("Name without Extension:", name) // Output: "image"
}

The above code separates the base name image and the extension .png from the file path.

Conclusion

By using the path/filepath package, you can easily manipulate file paths to extract either the full filename or just the extension. This is particularly useful for applications that need to process different types of files and require their names or extensions for validation or categorization.

Next Article: Go: How to get the size of a file (in KB or mB)

Previous Article: How to zip/ unzip a file or folder in Go

Series: File I/O and Operating System Interaction

Golang

Related Articles

You May Also Like

  • How to remove HTML tags in a string in Go
  • How to remove special characters in a string in Go
  • How to remove consecutive whitespace in a string in Go
  • How to count words and characters in a string in Go
  • Relative imports in Go: Tutorial & Examples
  • How to run Python code with Go
  • How to generate slug from title in Go
  • How to create an XML sitemap in Go
  • How to redirect in Go (301, 302, etc)
  • Using Go with MongoDB: CRUD example
  • Auto deploy Go apps with CI/ CD and GitHub Actions
  • Fixing Go error: method redeclared with different receiver type
  • Fixing Go error: copy argument must have slice type
  • Fixing Go error: attempted to use nil slice
  • Fixing Go error: assignment to constant variable
  • Fixing Go error: cannot compare X (type Y) with Z (type W)
  • Fixing Go error: method has pointer receiver, not called with pointer
  • Fixing Go error: assignment mismatch: X variables but Y values
  • Fixing Go error: array index must be non-negative integer constant