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
+7.7k Golang : Detect sample rate, channels or latency with PortAudio
+5.1k Google : Block or disable caching of your website content
+6.2k Golang : Experimenting with the Rejang script
+12.5k Golang : Print UTF-8 fonts on image example
+10.7k Golang : Generate 403 Forbidden to protect a page or prevent indexing by search engine
+10.2k Golang : Test a slice of integers for odd and even numbers
+17.1k Golang : How to generate QR codes?
+17.6k Golang : Linked list example
+12.3k Golang : calculate elapsed run time
+42.2k Golang : How do I convert int to uint8?
+7.8k Golang : Error reading timestamp with GORM or SQL driver
+7k Mac OSX : Find large files by size