Skip to content

Commit 4ce05a3

Browse files
author
woytu
committed
路径标准化拼接
1 parent f8beb1c commit 4ce05a3

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

utils/file.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package utils
22

33
import (
4+
"errors"
45
"fmt"
56
"io/ioutil"
67
"log"
@@ -291,7 +292,6 @@ func ParentDirectory(dirctory string) string {
291292
* @date 2019/6/28 15:53
292293
*/
293294
func CurrentDirectory() string {
294-
// sep := string(os.PathSeparator)
295295
return strings.Replace(OsPath(), "\\", "/", -1)
296296
}
297297

@@ -301,11 +301,34 @@ func CurrentDirectory() string {
301301
* @author claer woytu.com
302302
* @date 2019/6/29 3:22
303303
*/
304-
func ContextPath(root string) string {
304+
func ContextPath(root string) (path string, err error) {
305305
// 获取当前绝对路径
306306
dir, err := os.Getwd()
307307
if err != nil {
308308
log.Fatal(err)
309309
}
310-
return Substring(dir, 0, strings.LastIndex(dir, root)+len(root))
310+
index := strings.LastIndex(dir, root)
311+
if len(dir) < len(root) || index <= 0 {
312+
err = errors.New("错误:路径不正确")
313+
return dir, err
314+
}
315+
return dir[0 : strings.LastIndex(dir, root)+len(root)], nil
316+
}
317+
318+
/**
319+
* 路径标准化拼接
320+
*
321+
* @param paths 可变路径参数
322+
* @return
323+
* @author claer woytu.com
324+
* @date 2019/6/29 3:46
325+
*/
326+
func PathStitching(paths ...string) string {
327+
sep := string(os.PathSeparator)
328+
path := ""
329+
for _, value := range paths {
330+
fmt.Println(path)
331+
path = path + sep + value
332+
}
333+
return path[1:]
311334
}

0 commit comments

Comments
 (0)