ADVERTISEMENT
ADVERTISEMENT

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

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?

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 »



ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.