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
+6.8k How to let Facebook Login button redirect to a particular URL ?
+14.8k Golang : How to check for empty array string or string?
+6.4k Grep : How to grep for strings inside binary data
+12.3k Golang : Encrypt and decrypt data with x509 crypto
+55.1k Golang : Unmarshal JSON from http response
+23.7k Golang : Fix type interface{} has no field or no methods and type assertions example
+17.6k How to enable MariaDB/MySQL logs ?
+12.1k Golang : calculate elapsed run time
+10k Golang : Identifying Golang HTTP client request
+20.4k Nginx + FastCGI + Go Setup.
+5.7k Cash Flow : 50 days to pay your credit card debt