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
+20.4k Golang : GORM create record or insert new record into database example
+8k Golang : Gorilla web tool kit schema example
+12.7k Golang : Activate web camera and broadcast out base64 encoded images
+19.4k Golang : Secure(TLS) connection between server and client
+5.1k Facebook : How to force facebook to scrape latest URL link data?
+4.1k Golang : Experimental Jawi programming language
+17.7k Golang : Set, Get and List environment variables
+9.7k Golang : Underscore string example
+4.5k Golang : The Tao of importing package
+6k How to let Facebook Login button redirect to a particular URL ?
+24.8k Golang : missing Mercurial command
+11.4k Golang : calculate elapsed run time