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.4k Grep : How to grep for strings inside binary data
+6.5k Golang : Rename part of filename
+12.3k Golang : Verify token from Google Authenticator App
+32.6k Golang : Call a function after some delay(time.Sleep and Tick)
+7.4k Golang : Ackermann function example
+7.1k Findstr command the Grep equivalent for Windows
+12.8k Golang : Fix cannot use buffer (type bytes.Buffer) as type io.Writer(Write method has pointer receiver) error
+10.1k Golang : Characters limiter example
+7.2k Golang : Auto-generate reply email with text/template package
+7.6k Golang : Another camera capture GUI application with GTK and OpenCV
+7.8k Golang : Gonum standard normal random numbers example