Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

import "os"

Current Working Directory

path, err := os.Getwd()
path
/workdir/archives/blog/misc/content/2022/07/get-the-current-working-directory-in-golang

Path of the Golang Executable

path, err := os.Executable()
path
/usr/local/go/bin/gophernotes

Home Directory of the Current User

UserHomeDir returns the current (real) user’s home directory. If the code is run by a non-root user named some_user on Linux, then the returned home directory is /home/some_user. However, if some_user has sudo access and run the code using sudo, then the real user user is root and the returned home directory is /root.

path, err := os.UserHomeDir()
path
/home/dclong