filepath.Join删除点 [英] filepath.Join removes dot

查看:81
本文介绍了filepath.Join删除点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对rsync的创建路径有问题.

i have a problem with create path for rsync.

x := filepath.Join("home", "my_name", "need_folder", ".")
fmt.Println(x)

我得到了"home/my_name/need_folder",但是需要"home/my_name/need_folder/.",如果没有concat怎么解决?在名称为."的linux文件夹中.并非不可能.

I get "home/my_name/need_folder", but need "home/my_name/need_folder/.", how fix without concat? In linux folder with name "." not impossible.

谢谢!

推荐答案

使用

You can't do that with the filepath.Join() as its documentation states:

加入通话以清除结果...

Join calls Clean on the result...

,并且由于.表示当前"值,因此目录,它将被 filepath.Clean() :

And since . denotes the "current" directory, it will be removed by filepath.Clean():

它会反复应用以下规则,直到无法进行进一步处理为止:

It applies the following rules iteratively until no further processing can be done:

  1. [...]

  1. [...]

消除每个.路径名元素(当前目录).

Eliminate each . path name element (the current directory).

实际上,您无法使用 path/filepath 包来做您想做的事情完全不支持此操作.

And in fact you can't do what you want with the path/filepath package at all, there is no support for this operation.

您需要手动使用字符串连接.为此使用filepath.Separator会很安全:

You need to use string concatenation manually. Use filepath.Separator for it, it'll be safe:

x := filepath.Join("home", "my_name", "need_folder") +
    string(filepath.Separator) + "."
fmt.Println(x)

输出(在转到游乐场上尝试):

home/my_name/need_folder/.

这篇关于filepath.Join删除点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆