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
+23k Golang : untar or extract tar ball archive example
+21.9k Golang : Encrypt and decrypt data with TripleDES
+13.6k Golang : Increment string example
+9.6k Golang : How to get username from email address
+14.5k Golang : Recombine chunked files example
+6.9k Golang : Find the longest line of text example
+30.1k Golang : Record voice(audio) from microphone to .WAV file
+17.8k Golang : Parse date string and convert to dd-mm-yyyy format
+14.7k Golang : Overwrite previous output with count down timer
+7.3k Golang : Gargish-English language translator
+8.3k Golang : Multiplexer with net/http and map
+5k Which content-type(MIME type) to use for JSON data