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.2k Golang : Join lines with certain suffix symbol example
+5k Linux/MacOSX : Search for files by filename and extension with find command
+14.3k Golang : invalid character ',' looking for beginning of value
+26.4k Golang : Move file to another directory
+8.5k Golang : Eroding and dilating image with OpenCV example
+17.7k Golang : convert int to string
+5.5k Golang : Calculate diameter, circumference, area, sphere surface and volume
+14.5k Golang : Read large file with bufio.Scanner cause token too long error
+10.8k Golang : Print UTF-8 fonts on image example
+7.3k Golang : Convert word to its plural form example
+6.7k Gogland : Where to put source code files in package directory for rookie
+3.7k JavaScript: Add marker function on Google Map