×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

How to get the file name extension used by path in Golang?

Given a path, we have to find the file name extension used by the path in Golang.
Submitted by IncludeHelp, on October 27, 2021 [Last updated : March 05, 2023]

Getting the file name extension used by path in Golang

In the Go programming language, to get the file name extension used by the given path – we use the Ext() function of path/filepath package. The Ext() function returns the file name extension used by the given path. The extension is the suffix beginning at the final dot in the final element of the given path; it is empty if there is no dot.

Syntax:

func Ext(path string) string

Consider the below Golang program demonstrating get the file name extension used by the given path?

Golang code to get the file name extension used by path

package main

import (
	"fmt"
	"path/filepath"
)

func main() {
	ext1 := filepath.Ext("/tmp/test.go")
	fmt.Println("ext1:", ext1)

	ext2 := filepath.Ext("./picture1.jpg")
	fmt.Println("ext2:", ext2)
}

Output

ext1: .go
ext2: .jpg

Golang path/filepath Package Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.