duaiwo9093 2018-05-15 19:27
浏览 238
已采纳

更改文件名路径目录

I need to change filename paths relative to a given folder.
I am dealing with multi-user shared buckets and it would be nice for the user not to know the complete file path.

I have an example below but it seems a bit dirty.

package main

import (
    "fmt"
    "strings"
)

func main() {
    secretUrl := "allusers/user/home/path/to/file"
    separator := "home/"

    newUrl := strings.Split(secretUrl, separator)

    newUserUrl := separator + newUrl[len(newUrl)-1]
    fmt.Printf("%q
", newUserUrl)

}

Also, the new path needs to start from the separating point.

Is there another way to do this more elegantly? Examples would be appreciated.

  • 写回答

2条回答 默认 最新

  • dongsui0929 2018-05-15 21:11
    关注

    I think a nice platform-independent solution is to use the golang path/filepath Split function. Does not make assumptions about what path separators look like, handles volumes, etc...

    import (
        "path/filepath"
    )
    
    func subpath(homeDir, prevDir string) string {
        subFiles := ""
        for {
            dir, file := filepath.Split(prevDir)
            if len(subFiles) > 0 {
                subFiles = file + string(filepath.Separator) + subFiles
            } else {
                subFiles = file
            }
            if file == homeDir {
                break
            }
            if len(dir) == 0 || dir == prevDir {
                break
            }
            prevDir = dir[:len(dir) - 1]
        }
        return subFiles
    }
    

    Call with

    subpath("home", "allusers/user/home/path/to/file")
    

    To handle cases where "home" may appear more than once and you wish to match the first:

    func subpath(homeDir, prevDir string) (subFiles string, found bool) {
        for {
            dir, file := filepath.Split(prevDir)
            if len(subFiles) > 0 {
                subFiles = file + string(filepath.Separator) + subFiles
            } else {
                subFiles = file
            }
            if len(dir) == 0 || dir == prevDir {
                return
            }
            prevDir = dir[:len(dir) - 1]
            if file == homeDir {
                found = true
                // look for it lower down
                lower, foundAgain := subpath(homeDir, prevDir)
                if foundAgain {
                    subFiles = lower + string(filepath.Separator) + subFiles
                }
                return
            }
        }
    }
    

    Call with

    path, found = subpath("home", "allusers/user/home/path/home2/home/to/file")
    if found {
        fmt.Printf("%q
    ", path)
    } else {
        fmt.Printf("not found
    ")
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 运筹学中在线排序的时间在线排序的在线LPT算法
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧