diff --git a/docs/fileutil.md b/docs/fileutil.md index 22f4f81..9cd04d3 100644 --- a/docs/fileutil.md +++ b/docs/fileutil.md @@ -26,6 +26,7 @@ import ( - [CreateFile](#CreateFile) - [CreateDir](#CreateDir) - [CopyFile](#CopyFile) +- [CurrentPath](#CurrentPath) - [FileMode](#FileMode) - [MiMeType](#MiMeType) - [IsExist](#IsExist) @@ -151,6 +152,32 @@ func main() { } ``` +### CurrentPath + +

return current absolute path.

+ +Signature: + +```go +func CurrentPath() string +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/fileutil" +) + +func main() { + absPath := CurrentPath() + fmt.Println(absPath) +} +``` + ### FileMode

Return file mode infomation.

diff --git a/docs/fileutil_zh-CN.md b/docs/fileutil_zh-CN.md index 88247bf..180cc02 100644 --- a/docs/fileutil_zh-CN.md +++ b/docs/fileutil_zh-CN.md @@ -26,6 +26,7 @@ import ( - [CreateFile](#CreateFile) - [CreateDir](#CreateDir) - [CopyFile](#CopyFile) +- [CurrentPath](#CurrentPath) - [FileMode](#FileMode) - [MiMeType](#MiMeType) - [IsExist](#IsExist) @@ -150,6 +151,32 @@ func main() { } ``` +### CurrentPath + +

返回当前位置的绝对路径。

+ +函数签名: + +```go +func CurrentPath() string +``` + +示例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/fileutil" +) + +func main() { + absPath := CurrentPath() + fmt.Println(absPath) +} +``` + ### FileMode

获取文件mode信息

diff --git a/fileutil/file.go b/fileutil/file.go index ab5fcc9..c48a8b4 100644 --- a/fileutil/file.go +++ b/fileutil/file.go @@ -15,6 +15,7 @@ import ( "os" "path" "path/filepath" + "runtime" "strings" ) @@ -345,3 +346,15 @@ func MiMeType(file any) string { } return mediatype } + +// CurrentPath return current absolute path. +// Play: todo +func CurrentPath() string { + var absPath string + _, filename, _, ok := runtime.Caller(1) + if ok { + absPath = path.Dir(filename) + } + + return absPath +} diff --git a/fileutil/file_test.go b/fileutil/file_test.go index 14407db..acb40e6 100644 --- a/fileutil/file_test.go +++ b/fileutil/file_test.go @@ -241,3 +241,8 @@ func TestListFileNames(t *testing.T) { expected := []string{"formatter.go", "formatter_example_test.go", "formatter_test.go"} assert.Equal(expected, filesInPath) } + +func TestCurrentPath(t *testing.T) { + absPath := CurrentPath() + t.Log(absPath) +}