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
+20.1k Golang : Check if os.Stdin input data is piped or from terminal
+4.7k Javascript : How to get width and height of a div?
+22.4k Generate checksum for a file in Go
+37.9k Golang : Read a text file and replace certain words
+5.2k Golang : Pad file extension automagically
+9.5k Javascript : Read/parse JSON data from HTTP response
+25.6k Golang : missing Mercurial command
+8k Golang : Tell color name with OpenCV example
+14.4k Golang : Reset buffer example
+7.1k Golang : Check if one string(rune) is permutation of another string(rune)
+26.6k Golang : Convert file content into array of bytes
+16.7k Golang : Get the IPv4 and IPv6 addresses for a specific network interface