Standard library / os

os.Symlink()

Creates a symbolic link storing the supplied target path; target need not exist. Windows support and privileges vary. Example uses a disposable temporary workspace. Expected stdout: 'abc\n'.

Formal Syntax & Structure

go syntaxgo
func Symlink(oldname, newname string) error

Parameters & Attributes

NameTypeDescription
oldnamestringExisting path or stored symlink target.
newnamestringNew pathname.

Return Value

error

Creates a symbolic link storing the supplied target path; target need not exist.

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"))  b, e := os.ReadFile("alias")  check(e)  fmt.Println(string(b))}
Go Reference