Standard library / path

path.Split()

Splits immediately after the final slash without cleaning; the directory portion retains its trailing slash. Example uses a disposable temporary workspace. Expected stdout: 'docs/ go.txt\n'.

Formal Syntax & Structure

go syntaxgo
func Split(path string) (dir, file string)

Parameters & Attributes

NameTypeDescription
pathstringPathname interpreted according to this package.

Return Value

(dir, file string)

Splits immediately after the final slash without cleaning; the directory portion retains its trailing slash.

Working Example

Example Snippetgo
package main
import (  "fmt"  "os"  "path")
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()(); d, f := path.Split("docs/go.txt"); fmt.Println(d, f) }
Go Reference