Skip to content

Commit 669c146

Browse files
authored
Merge pull request #883 from devlights/add-filepath-exclude-suffix-example
2 parents 4b47e37 + aa5140a commit 669c146

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

examples/basic/filepaths/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
このディレクトリには以下のサンプルがあります。
44

5-
| file | example name | note |
6-
| ---------------- | ------------- | --------------------------------------------- |
7-
| filepath_walk.go | filepath_walk | filepaths.Walk() のサンプルです. |
8-
| filepath_glob.go | filepath_glob | filepath.Glob() の動作についてのサンプルです. |
9-
5+
| file | example name | note |
6+
| ----------------- | ----------------------- | ----------------------------------------------------- |
7+
| filepath_walk.go | filepath_walk | filepaths.Walk() のサンプルです. |
8+
| filepath_glob.go | filepath_glob | filepath.Glob() の動作についてのサンプルです. |
9+
| exclude_suffix.go | filepath_exclude_suffix | ファイル名から拡張子を除いた値を取得するサンプルです. |

examples/basic/filepaths/examples.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ func NewRegister() mapping.Register {
1515
func (r *register) Regist(m mapping.ExampleMapping) {
1616
m["filepath_walk"] = FilePathWalk
1717
m["filepath_glob"] = FilePathGlob
18+
m["filepath_exclude_suffix"] = ExcludeSuffix
1819
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package filepaths
2+
3+
import (
4+
"path/filepath"
5+
"strings"
6+
7+
"github.com/devlights/gomy/output"
8+
)
9+
10+
// ExcludeSuffix は、ファイル名から拡張子を除いた値を取得するサンプルです.
11+
//
12+
// filepath.Ext()とstrings.TrimSuffix()を組み合わせて取得出来ます。
13+
//
14+
// > strings.TrimSuffix(file, filepath.Ext(file))
15+
//
16+
// または、スライスを拡張子分だけカットすることでも取得出来ます。
17+
//
18+
// > file[:len(file)-len(filepath.Ext(file))]
19+
//
20+
// # REFERENCES
21+
// - https://pkg.go.dev/path/filepath@go1.23.4#Ext
22+
// - https://pkg.go.dev/strings@go1.23.4#TrimSuffix
23+
func ExcludeSuffix() error {
24+
var (
25+
fpath = "/path/to/src/something.go"
26+
dir = filepath.Dir(fpath)
27+
fname = filepath.Base(fpath)
28+
ext = filepath.Ext(fname)
29+
30+
base1 = strings.TrimSuffix(fname, ext)
31+
base2 = fname[:len(fname)-len(ext)]
32+
)
33+
output.Stdoutl("[fpath]", fpath)
34+
output.Stdoutl("[dir ]", dir)
35+
output.Stdoutl("[ext ]", ext)
36+
output.Stdoutl("[base1]", base1)
37+
output.Stdoutl("[base2]", base2)
38+
39+
return nil
40+
}

0 commit comments

Comments
 (0)