Standard library / path

path.Base()

Returns the final slash-separated element after ignoring trailing slashes; empty input becomes a dot. Example uses a disposable temporary workspace. Expected stdout: 'go.txt\n'.

Formal Syntax & Structure

go syntaxgo
func Base(path string) string

Parameters & Attributes

NameTypeDescription
pathstringPathname interpreted according to this package.

Return Value

string

Returns the final slash-separated element after ignoring trailing slashes; empty input becomes a dot.

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()(); fmt.Println(path.Base("/docs/go.txt/")) }
Go Reference