Last active
August 16, 2017 08:34
-
-
Save kemokemo/455579ce2698dc7b2c053703b91e7fe6 to your computer and use it in GitHub Desktop.
Go言語でパス情報から拡張子なしのファイル名を取り出したい ref: http://qiita.com/KemoKemo/items/d135ddc93e6f87008521
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ go run GetFileNameWithoutExt.go | |
path\to\file.name.hoge.name -> file.name.hoge | |
path\to\file.name -> file | |
path\to\file -> file |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
mattnさんにコメントでいただいた処理で修正しました!