Standard library / os
os.Readlink()
Returns the stored symbolic-link target, not the fully resolved target path. Example uses a disposable temporary workspace. Expected stdout: 'a.txt\n'.
Formal Syntax & Structure
go syntaxgo
func Readlink(name string) (string, error)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| name | string | Name or pathname of the target. |
Return Value
(string, error)
Returns the stored symbolic-link target, not the fully resolved target path.
Working Example
Example Snippetgo
package main
import ( "fmt" "os")
func check(e error) { if e != nil { panic(e) }}func seed() { check(os.WriteFile("a.txt", []byte("abc"), 0600)) }func workspace() func() { root, e := os.MkdirTemp("", "bytemind-go-") check(e) old, e := os.Getwd() check(e) check(os.Chdir(root)) return func() { check(os.Chdir(old)); check(os.RemoveAll(root)) }}func main() { defer workspace()() seed() check(os.Symlink("a.txt", "alias")) p, e := os.Readlink("alias") check(e) fmt.Println(p)}