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.2k Golang : Join lines with certain suffix symbol example
+13.5k Golang : Execute function at intervals or after some delay
+7.1k Golang : Find relative luminance or color brightness
+5.5k Golang : Calculate diameter, circumference, area, sphere surface and volume
+7.3k Swift : Convert (cast) Character to Integer?
+10.8k Golang : Simple client-server HMAC authentication without SSL example
+5.2k Golang : Process non-XML/JSON formatted ASCII text file example
+10.8k Golang : Save webcamera frames to video file
+30.1k Golang : Math pow(the power of x^y) example
+17.9k Golang : Clearing slice
+18.3k Golang : Count JSON objects and convert to slice/array
+13.2k Golang : Reset buffer example