Golang : Create and resolve(read) symbolic links
Creating new symbolic link for a file and resolving(read) symbolic link back to the origin file is pretty straight forward in Golang with the os.Symlink
and os.Readlink
functions.
Here is an example on how to create and resolve symbolic link.
package main
import (
"fmt"
"os"
)
func main() {
// create a new symbolic or "soft" link
err := os.Symlink("file.txt", "file-symlink.txt")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// resolve symlinks
fileInfo, err := os.Lstat("file-symlink.txt")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if fileInfo.Mode()&os.ModeSymlink != 0 {
originFile, err := os.Readlink(fileInfo.Name())
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Resolved symlink to : ", originFile)
}
}
Sample output :
Resolved symlink to : file.txt
References :
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+5.9k Golang : How to generate Code 39 barcode?
+5.4k SSL : How to check if current certificate is sha1 or sha2 from command line
+5k Golang : Find the length of big.Int variable example
+3.6k Javascript : How to replace HTML inside <div>?
+2k Golang : Converting individual Jawi alphabet to Rumi(Romanized) alphabet example
+3.8k Get website traffic ranking with Similar Web or Alexa
+17.2k Golang : How to get time zone and load different time zone?
+6.9k Golang : Sort and reverse sort a slice of floats
+35.3k Golang : How to iterate over a []string(array)
+5.6k Golang : Get curl -I or head data from URL example
+12.6k Golang : invalid character ',' looking for beginning of value
+12.8k Golang : How to get own program name during runtime ?