1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52:28 +08:00

feat: add CurrentPath

This commit is contained in:
dudaodong
2023-04-05 15:55:55 +08:00
parent 3685aee02b
commit 6e7300bbbf
4 changed files with 72 additions and 0 deletions

View File

@@ -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() {
}
```
### <span id="CurrentPath">CurrentPath</span>
<p>return current absolute path.</p>
<b>Signature:</b>
```go
func CurrentPath() string
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/fileutil"
)
func main() {
absPath := CurrentPath()
fmt.Println(absPath)
}
```
### <span id="FileMode">FileMode</span>
<p>Return file mode infomation.</p>

View File

@@ -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() {
}
```
### <span id="CurrentPath">CurrentPath</span>
<p>返回当前位置的绝对路径。</p>
<b>函数签名:</b>
```go
func CurrentPath() string
```
<b>示例:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/fileutil"
)
func main() {
absPath := CurrentPath()
fmt.Println(absPath)
}
```
### <span id="FileMode">FileMode</span>
<p>获取文件mode信息</p>

View File

@@ -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
}

View File

@@ -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)
}