Skip to content

Instantly share code, notes, and snippets.

@kemokemo
Last active August 16, 2017 08:34
Show Gist options
  • Save kemokemo/455579ce2698dc7b2c053703b91e7fe6 to your computer and use it in GitHub Desktop.
Save kemokemo/455579ce2698dc7b2c053703b91e7fe6 to your computer and use it in GitHub Desktop.
Go言語でパス情報から拡張子なしのファイル名を取り出したい ref: http://qiita.com/KemoKemo/items/d135ddc93e6f87008521
$ go run GetFileNameWithoutExt.go
path\to\file.name.hoge.name -> file.name.hoge
path\to\file.name -> file
path\to\file -> file
package main
import (
"fmt"
"path/filepath"
)
func main() {
paths := []string{
filepath.Join("path", "to", "file.name.hoge.name"),
filepath.Join("path", "to", "file.name"),
filepath.Join("path", "to", "file"),
}
for _, path := range paths {
filename := getFileNameWithoutExt(path)
fmt.Printf("%s -> %s\n", path, filename)
}
}
func getFileNameWithoutExt(path string) string {
// Fixed with a nice method given by mattn-san
return filepath.Base(path[:len(path)-len(filepath.Ext(path))])
}
@kemokemo
Copy link
Author

mattnさんにコメントでいただいた処理で修正しました!

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