Created
April 24, 2021 13:59
-
-
Save developer-guy/b43b048fbeb72cca168e65045a49055c to your computer and use it in GitHub Desktop.
Creating&Resolving symlinks in Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"os" | |
"path/filepath" | |
) | |
func main() { | |
dir, err := os.Getwd() | |
if err != nil { | |
log.Fatalf("Could not get working directory: %v", err) | |
} | |
err = os.Chmod(dir+"/executable.sh", 0777) | |
if err != nil { | |
log.Fatalf("Could not chmod of the file: %v", err) | |
} | |
// create a new symbolic or "soft" link | |
symLinkPath := "/usr/local/bin/" | |
symLinkFile := "hello" | |
err = os.Symlink(dir+"/executable.sh", filepath.Join(symLinkPath+symLinkFile)) | |
if err != nil { | |
log.Fatalf("Could not create symlink: %v", err) | |
} | |
// resolve symlinks | |
fileInfo, err := os.Lstat(filepath.Join(symLinkPath + symLinkFile)) | |
if err != nil { | |
log.Fatalf("Could not stat symlink: %v", err) | |
} | |
if fileInfo.Mode()&os.ModeSymlink != 0 { | |
originFile, err := os.Readlink(filepath.Join(symLinkPath + fileInfo.Name())) | |
if err != nil { | |
log.Fatalf("Could not read symlink: %v", err) | |
} | |
fmt.Println("Resolved symlink to : ", originFile) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment