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.1k Golang : Get missing location after unmarshal binary and gob decode time.
+11.7k Golang : Skip blank/empty lines in CSV file and trim whitespaces example
+5.2k Golang : Detect face in uploaded photo like GPlus
+3.9k PHP : Extract part of a string starting from the middle
+8.8k Golang : Translate language with language package example
+16.1k Golang : Find smallest number in array
+9.2k Golang : Convert file unix timestamp to UTC time example
+6.5k SSL : How to check if current certificate is sha1 or sha2 from command line
+6.3k Android Studio : How to detect camera, activate and capture example
+4.6k Golang : What is StructTag and how to get StructTag's value?
+25.8k Golang : Convert CSV data to JSON format and save to file