Standard library / path/filepath

path/filepath.Localize()

Converts a valid io/fs slash path to an OS path if representable; the result is local. Introduced in Go 1.23. Example uses a disposable temporary workspace. Expected stdout: 'docs/go.txt\n'.

Formal Syntax & Structure

go syntaxgo
func Localize(path string) (string, error)

Parameters & Attributes

NameTypeDescription
pathstringPathname interpreted according to this package.

Return Value

(string, error)

Converts a valid io/fs slash path to an OS path if representable; the result is local.

Working Example

Example Snippetgo
package main
import (  "fmt"  "os"  "path/filepath")
func check(e error) {  if e != nil {    panic(e)  }}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()()  p, e := filepath.Localize("docs/go.txt")  check(e)  fmt.Println(filepath.ToSlash(p))}
Go Reference