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
+11k Golang : Proper way to test CIDR membership of an IP 4 or 6 address example
+7.3k Golang : Gorrila set route name and get the current route name
+5k Golang : Issue HTTP commands to server and port example
+4.5k Linux : sudo yum updates not working
+8.9k Golang : How to use Gorilla webtoolkit context package properly
+20.9k Golang : Sort and reverse sort a slice of strings
+16.4k Golang : File path independent of Operating System
+6.7k Android Studio : Hello World example
+18.4k Golang : Send email with attachment
+13k Golang : How to get a user home directory path?
+11.5k Golang : Surveillance with web camera and OpenCV