Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions examples/basic/filepaths/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

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

| file | example name | note |
| ---------------- | ------------- | --------------------------------------------- |
| filepath_walk.go | filepath_walk | filepaths.Walk() のサンプルです. |
| filepath_glob.go | filepath_glob | filepath.Glob() の動作についてのサンプルです. |

| file | example name | note |
| ----------------- | ----------------------- | ----------------------------------------------------- |
| filepath_walk.go | filepath_walk | filepaths.Walk() のサンプルです. |
| filepath_glob.go | filepath_glob | filepath.Glob() の動作についてのサンプルです. |
| exclude_suffix.go | filepath_exclude_suffix | ファイル名から拡張子を除いた値を取得するサンプルです. |
1 change: 1 addition & 0 deletions examples/basic/filepaths/examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ func NewRegister() mapping.Register {
func (r *register) Regist(m mapping.ExampleMapping) {
m["filepath_walk"] = FilePathWalk
m["filepath_glob"] = FilePathGlob
m["filepath_exclude_suffix"] = ExcludeSuffix
}
40 changes: 40 additions & 0 deletions examples/basic/filepaths/exclude_suffix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package filepaths

import (
"path/filepath"
"strings"

"github.com/devlights/gomy/output"
)

// ExcludeSuffix は、ファイル名から拡張子を除いた値を取得するサンプルです.
//
// filepath.Ext()とstrings.TrimSuffix()を組み合わせて取得出来ます。
//
// > strings.TrimSuffix(file, filepath.Ext(file))
//
// または、スライスを拡張子分だけカットすることでも取得出来ます。
//
// > file[:len(file)-len(filepath.Ext(file))]
//
// # REFERENCES
// - https://pkg.go.dev/path/filepath@go1.23.4#Ext
// - https://pkg.go.dev/strings@go1.23.4#TrimSuffix
func ExcludeSuffix() error {
var (
fpath = "/path/to/src/something.go"
dir = filepath.Dir(fpath)
fname = filepath.Base(fpath)
ext = filepath.Ext(fname)

base1 = strings.TrimSuffix(fname, ext)
base2 = fname[:len(fname)-len(ext)]
)
output.Stdoutl("[fpath]", fpath)
output.Stdoutl("[dir ]", dir)
output.Stdoutl("[ext ]", ext)
output.Stdoutl("[base1]", base1)
output.Stdoutl("[base2]", base2)

return nil
}
Loading