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
+14.6k Golang : Rename directory
+12.9k Swift : Convert (cast) Int or int32 value to CGFloat
+8k Golang : Gomobile init produce "iphoneos" cannot be located error
+10.8k Golang : Underscore string example
+4.8k MariaDB/MySQL : Form select statement or search query with Chinese characters
+6.6k Golang : Totalize or add-up an array or slice example
+18.9k Golang : How to make function callback or pass value from function as parameter?
+7.5k Golang : Convert source code to assembly language
+5.5k How to check with curl if my website or the asset is gzipped ?
+6.1k PHP : How to check if an array is empty ?
+22.8k Golang : Round float to precision example
+7.8k Golang : Error reading timestamp with GORM or SQL driver