urls

urls.JoinPath

把给定的元素拼接成一个 URL 字符串,并清理结果中的 ./ 与 ../ 元素;参数列表为空时返回空字符串。

签名
urls.JoinPath ELEMENT...
返回类型
string

这一页解决什么问题

需要在模板里把几段拼成一个地址(片段路径、域名加路径、从数据文件里读来的相对路径),又不想自己处理多余斜杠和 ../。urls.JoinPath 负责拼接并清理 .、..,而且与操作文件系统路径的 path.Join 不同,它保留开头的连续斜杠(协议相对地址 //host/x 需要这一点)。

什么时候用,什么时候别用

该用:

  • 拼接 URL 的各个片段,尤其是片段里可能出现 ../;
  • 需要协议相对地址(//example.org)时。

别用:

用法

go-html-template
{{ urls.JoinPath }} → "" (empty string)
{{ urls.JoinPath "" }} → /
{{ urls.JoinPath "a" }} → a
{{ urls.JoinPath "a" "b" }} → a/b
{{ urls.JoinPath "/a" "b" }} → /a/b
{{ urls.JoinPath "https://example.org" "b" }} → https://example.org/b

{{ urls.JoinPath (slice "a" "b") }} → a/b

与 path.Join 函数不同,urls.JoinPath 会保留开头连续的斜杠。

完整示例(实测)

layouts/_partials/build-url.htmlgo-html-template
[{{ urls.JoinPath }}]|[{{ urls.JoinPath "" }}]|[{{ urls.JoinPath "a" "b" }}]|[{{ urls.JoinPath "/a" "b" }}]|[{{ urls.JoinPath "//a" "b" }}]|[{{ urls.JoinPath "../a" "b" }}]

Hugo 0.167.0 实测输出:

[]|[/]|[a/b]|[/a/b]|[//a/b]|[a/b]

你应当看到什么:空参数列表得到空字符串;单个空字符串得到 /;//a 开头的双斜杠被保留(//a/b),这是它与 path.Join 的分水岭;而 ../a 里的 .. 被清理掉,得 a/b。这些都与上游示例一致。

返回值边界(实测)

测量条件:Hugo 0.167.0 extended,Windows;baseURL 取 https://example.org/ 或 https://example.org/docs/ 时结果相同(本函数不读 baseURL)。

输入 结果 是否报错
无参数 ""(空字符串) 否
"" / 否
"a" "b" a/b 否
"/a" "b" /a/b 否
"//a" "b" //a/b(保留开头连续斜杠) 否
"../a" "b" a/b(.、.. 被清理) 否
"https://example.org" "b" https://example.org/b 否
(slice "a" "b") a/b(切片作为单个参数传入) 否
返回类型 string 否

常见坑

类别 症状 真因 怎么修
没报错但结果不对 传空字符串想得到空串,却得到 / 上游即如此:只有参数列表为空才返回 ""(实测 urls.JoinPath "" → /) 空值先用 with 判断,或直接不传参
没报错但结果不对 用 JoinPath 拼文件路径,结果多出斜杠 本函数面向 URL,保留 // 是有意为之 文件路径用 path.Join
没报错但结果不对 结果里没有域名前缀 本函数只做拼接,不读 baseURL 需要带域名的完整地址用 urls.AbsURL

更多排查入口见故障排查。