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
+9.9k Golang : Simple Jawi(Yawi) to Rumi(Latin/Romanize) converter
+24.6k Golang : Storing cookies in http.CookieJar example
+18.1k Golang : Write file with io.WriteString
+6.7k CloudFlare : Another way to get visitor's real IP address
+15.8k Golang : convert string or integer to big.Int type
+5.1k Golang : Get S3 or CloudFront object or file information
+8.4k Golang : Gaussian blur on image and camera video feed examples
+5.7k Linux/MacOSX : Search for files by filename and extension with find command
+22k Golang : How to read JPG(JPEG), GIF and PNG files ?
+8.5k Golang : GMail API create and send draft with simple upload attachment example
+8.3k Android Studio : Image button and button example
+20.4k Golang : How to force compile or remove object files first before rebuild?