Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,561 questions

56,774 answers

573 users

How to remove file extension in Go

2 Answers

0 votes
package main

import (
	"fmt"
	"path/filepath"
	"strings"
)

/*
RemoveFileExtension removes the last file extension from a filename or path.

Examples:
  document.pdf      -> document
  archive.tar.gz    -> archive.tar
  photo.jpg         -> photo
  README            -> README
  /tmp/file.txt     -> /tmp/file

Parameters:
  path - The filename or path.

Returns:
  The filename/path without its last extension.
*/
func RemoveFileExtension(path string) string {
	// Get the last extension, including the leading dot.
	ext := filepath.Ext(path)

	// No extension found.
	if ext == "" {
		return path
	}

	// Remove the extension from the end of the path.
	return strings.TrimSuffix(path, ext)
}

func main() {
	fmt.Println(RemoveFileExtension("document.pdf"))
	fmt.Println(RemoveFileExtension("archive.tar.gz"))
	fmt.Println(RemoveFileExtension("/tmp/file.txt"))
	fmt.Println(RemoveFileExtension("README"))
}


/*
run:

document
archive.tar
/tmp/file
README

*/

 



answered 3 days ago by avibootz
0 votes
package main

import (
	"path/filepath"
    "fmt"
)

/*
RemoveFileExtension removes the last extension from a filename or path.
*/
func RemoveFileExtension(path string) string {
	ext := filepath.Ext(path)

	return path[:len(path)-len(ext)]
}

func main() {
	fmt.Println(RemoveFileExtension("document.pdf"))
	fmt.Println(RemoveFileExtension("archive.tar.gz"))
	fmt.Println(RemoveFileExtension("/tmp/file.txt"))
	fmt.Println(RemoveFileExtension("README"))
}


/*
run:

document
archive.tar
/tmp/file
README

*/

 



answered 3 days ago by avibootz
...