Jay Taylor's notes
back to listing indexGo: How to Get the Directory of the Current File
[web search]software engineering, the nature of good and evil, etc.
Go: How to Get the Directory of the Current File
In Python I often use the __file__
constant to get the directory of the current file (e.g., os.path.dirname(os.path.realpath(__file__))
). While writing a Go app recently, I found that there wasn’t an immediately obvious (or searchable, even with “golang”) equivalent.
But in the annals of the golong-nuts mailing list I eventually found out about runtime.Caller()
. This returns a few details about the current goroutine’s stack, including the file path.
The context of my problem was opening a data file in a shared package. What I cooked up was:
Sending 1
to runtime.Caller
identifies the caller of runtime.Caller
as the stack frame to return details about. So you you get info about the file your method is in. Check the docs for more in-depth coverage.
It’s not quite as elegant as __file__
but it works.
Popular
- An Epic Review of PyCharm 3 from a Vim User’s Perspective
- Using IPython Notebook with Django
- Set an ImageField path in Django manually
- Make Sublime Text 2 More like Vim: Wrap Code, Go To Last Edit, Jump Back, and More
- Go: How to Get the Directory of the Current File
- Instant Django Dev Environments with Tmux, Tmuxinator, and Virtualenvwrapper
- Four Things I Learned about Software Engineering from Programming MUDs as a Teenager
- How to Fix Slow Scrolling in Vim and MacVim on OS X
- Even Better GNU Screen
- Wrap Comments and Text to Column Width in IntelliJ Editors
Copyright © 2016 · Balance Theme on Genesis Framework · WordPress · Log in