Creating a relative symbolic link through the os package
<p>I would like to create a relative symbolic link in go using the os package.</p>

<p>os already <a href="https://golang.org/pkg/os/#Symlink" rel="noreferrer">contains the function: <code>os.SymLink(oldname, newname string)</code></a>, but it cannot create relative symlinks.</p>

<p>For example, if I run the following:</p>

<pre><code>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)
}
</code></pre>

<p>it creates the following in my filesystem:</p>

<pre><code>$ 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 -&gt; /tmp/rolfl/symexample/symtarget.txt
-rw-r--r-- 1 rolf rolf    6 Feb 21 15:21 symtarget.txt
</code></pre>

<p>How can I use golang to create the relative symlink that looks like:</p>

<pre><code>$ 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 -&gt; /tmp/rolfl/symexample/symtarget.txt
lrwxrwxrwx 1 rolf rolf   13 Feb 21 15:23 symrelative -&gt; symtarget.txt
-rw-r--r-- 1 rolf rolf    6 Feb 21 15:21 symtarget.txt
</code></pre>

<p>I want something that's like the <code>symrelative</code> above.</p>

<p>Do I have to resort to <code>os/exec</code>:</p>

<pre><code>cmd := exec.Command("ln", "-s", "symtarget.txt", "symlink")
cmd.Dir = "/tmp/rolfl/symexample"
cmd.CombinedOutput()
</code></pre>