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.4k Golang : ISO8601 Duration Parser example
+3.8k Linux : How to set root password in Linux Mint
+7.6k Linux/Unix : fatal: the Postfix mail system is already running
+9.8k Golang : Calculate Relative Strength Index(RSI) example
+8k Golang : Serving HTTP and Websocket from different ports in a program example
+12.4k Golang : Query string with space symbol %20 in between
+32.1k Golang : Converting a negative number to positive number
+8.2k Golang : Read file with ioutil
+22.6k Golang : Upload to S3 with official aws-sdk-go package
+6.9k Golang Hello World Example
+3.9k PHP : Extract part of a string starting from the middle