Standard library / os

os.UserCacheDir()

Returns the per-user cache directory, subject to OS and environment. Does not create it. Unix uses XDG_CACHE_HOME or HOME fallback. Example intended for Linux/AMD64 with Go 1.23.2 in a disposable working directory; other platforms may differ. Expected stdout: '/bytemind/cache\n'.

Formal Syntax & Structure

go syntaxgo
func UserCacheDir() (string, error)

Return Value

(string, error)

Returns the per-user cache directory, subject to OS and environment.

Working Example

Example Snippetgo
package main
import (  "fmt"  "os")
func must(e error) {  if e != nil {    panic(e)  }}func main() {  must(os.Setenv("XDG_CACHE_HOME", "/bytemind/cache"))  p, e := os.UserCacheDir()  must(e)  fmt.Println(p)}
Go Reference