英文:
Creating a relative symbolic link through the os package
问题
我想使用Go语言的os包创建一个相对符号链接。
os包已经包含了函数os.SymLink(oldname, newname string)
(https://golang.org/pkg/os/#Symlink),但它无法创建相对符号链接。
例如,如果我运行以下代码:
package main
import (
"io/ioutil"
"os"
"path/filepath"
)
func main() {
path := "/tmp/rolfl/symexample"
target := filepath.Join(path, "symtarget.txt")
os.MkdirAll(path, 0755)
ioutil.WriteFile(target, []byte("Hello\n"), 0644)
symlink := filepath.Join(path, "symlink")
os.Symlink(target, symlink)
}
它会在我的文件系统中创建以下内容:
$ ls -la /tmp/rolfl/symexample
total 12
drwxr-xr-x 2 rolf rolf 4096 Feb 21 15:21 .
drwxr-xr-x 3 rolf rolf 4096 Feb 21 15:21 ..
lrwxrwxrwx 1 rolf rolf 35 Feb 21 15:21 symlink -> /tmp/rolfl/symexample/symtarget.txt
-rw-r--r-- 1 rolf rolf 6 Feb 21 15:21 symtarget.txt
我该如何使用Go语言创建类似于以下的相对符号链接:
$ ln -s symtarget.txt symrelative
$ ls -la
total 12
drwxr-xr-x 2 rolf rolf 4096 Feb 21 15:23 .
drwxr-xr-x 3 rolf rolf 4096 Feb 21 15:21 ..
lrwxrwxrwx 1 rolf rolf 35 Feb 21 15:21 symlink -> /tmp/rolfl/symexample/symtarget.txt
lrwxrwxrwx 1 rolf rolf 13 Feb 21 15:23 symrelative -> symtarget.txt
-rw-r--r-- 1 rolf rolf 6 Feb 21 15:21 symtarget.txt
我想要的是像上面的symrelative
一样的相对符号链接。
我需要使用os/exec
吗?
cmd := exec.Command("ln", "-s", "symtarget.txt", "symlink")
cmd.Dir = "/tmp/rolfl/symexample"
cmd.CombinedOutput()
英文:
I would like to create a relative symbolic link in go using the os package.
os already contains the function: os.SymLink(oldname, newname string)
, but it cannot create relative symlinks.
For example, if I run the following:
package main
import (
"io/ioutil"
"os"
"path/filepath"
)
func main() {
path := "/tmp/rolfl/symexample"
target := filepath.Join(path, "symtarget.txt")
os.MkdirAll(path, 0755)
ioutil.WriteFile(target, []byte("Hello\n"), 0644)
symlink := filepath.Join(path, "symlink")
os.Symlink(target, symlink)
}
it creates the following in my filesystem:
$ ls -la /tmp/rolfl/symexample
total 12
drwxr-xr-x 2 rolf rolf 4096 Feb 21 15:21 .
drwxr-xr-x 3 rolf rolf 4096 Feb 21 15:21 ..
lrwxrwxrwx 1 rolf rolf 35 Feb 21 15:21 symlink -> /tmp/rolfl/symexample/symtarget.txt
-rw-r--r-- 1 rolf rolf 6 Feb 21 15:21 symtarget.txt
How can I use golang to create the relative symlink that looks like:
$ ln -s symtarget.txt symrelative
$ ls -la
total 12
drwxr-xr-x 2 rolf rolf 4096 Feb 21 15:23 .
drwxr-xr-x 3 rolf rolf 4096 Feb 21 15:21 ..
lrwxrwxrwx 1 rolf rolf 35 Feb 21 15:21 symlink -> /tmp/rolfl/symexample/symtarget.txt
lrwxrwxrwx 1 rolf rolf 13 Feb 21 15:23 symrelative -> symtarget.txt
-rw-r--r-- 1 rolf rolf 6 Feb 21 15:21 symtarget.txt
I want something that's like the symrelative
above.
Do I have to resort to os/exec
:
cmd := exec.Command("ln", "-s", "symtarget.txt", "symlink")
cmd.Dir = "/tmp/rolfl/symexample"
cmd.CombinedOutput()
答案1
得分: 19
在调用os.Symlink
时,不要包含对symtarget.txt
的绝对路径;只在写入文件时使用它:
package main
import (
"io/ioutil"
"os"
"path/filepath"
)
func main() {
path := "/tmp/rolfl/symexample"
target := "symtarget.txt"
os.MkdirAll(path, 0755)
ioutil.WriteFile(filepath.Join(path, "symtarget.txt"), []byte("Hello\n"), 0644)
symlink := filepath.Join(path, "symlink")
os.Symlink(target, symlink)
}
英文:
Don't include the absolute path to symtarget.txt
when calling os.Symlink
; only use it when writing to the file:
package main
import (
"io/ioutil"
"os"
"path/filepath"
)
func main() {
path := "/tmp/rolfl/symexample"
target := "symtarget.txt"
os.MkdirAll(path, 0755)
ioutil.WriteFile(filepath.Join(path, "symtarget.txt"), []byte("Hello\n"), 0644)
symlink := filepath.Join(path, "symlink")
os.Symlink(target, symlink)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论