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 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程