Skip to content

Instantly share code, notes, and snippets.

@ivanzoid
Last active March 11, 2025 16:08
Show Gist options
  • Save ivanzoid/129460aa08aff72862a534ebe0a9ae30 to your computer and use it in GitHub Desktop.
Save ivanzoid/129460aa08aff72862a534ebe0a9ae30 to your computer and use it in GitHub Desktop.
package main
import (
"strings"
"path/filepath"
)
func fileNameWithoutExtension(fileName string) string {
return strings.TrimSuffix(fileName, filepath.Ext(fileName))
}
@dxps
Copy link

dxps commented Mar 11, 2025

If the extension is static (always having a certain value), then a simple strings.CutSuffix(_,_) does the job. Example:

filenameWithoutExt, _ := strings.CutSuffix(filename, ".go")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment