gpt4 book ai didi

path - 结合绝对路径和相对路径得到新的绝对路径

转载 作者:IT老高 更新时间:2023-10-28 13:05:41 25 4
gpt4 key购买 nike

我正在编写一个程序,其中一个组件必须能够采用给定的路径(例如 /help/index.html/help/)和基于该位置的相对路径(例如 ../otherpage/index.htmlsub/dir/of/help/,或help2.html) 并生成相对路径隐含的绝对路径。考虑以下目录树。

/
index.html
content.txt
help/
help1.html
help2.html

文件 index.html 包含一个类似 help/help1.html 的链接。程序通过//index.html,结合help/help1.html得到/help/help1 .html.

同样,文件/help/help1.html有链接../content.txt,程序需要从该链接返回/content。 txt。有没有合理的方法来做到这一点?

谢谢。 :)

编辑:感谢斯蒂芬·温伯格!为大家from the future ,这是我使用的代码。

func join(source, target string) string {
if path.IsAbs(target) {
return target
}
return path.Join(path.Dir(source), target)
}

最佳答案

path.Joinpath.Dir 一起使用时应该可以满足您的要求。见 http://golang.org/pkg/path/#example_Join一个交互式示例。

path.Join(path.Dir("/help/help1.html"), "../content.txt")

这将返回 /content.txt

关于path - 结合绝对路径和相对路径得到新的绝对路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13078314/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com