问题答案 32026年7月9日 17:34
How to get the directory of the currently running file
In Golang, you can use the function from the standard library to obtain the full path of the currently executing executable file, and then use the function from the standard library to extract the directory from the full path.Here is an example code snippet demonstrating how to obtain the directory of the currently running file:In this example, we first call the function to retrieve the full path of the executable. If successful, it returns the path string and error. In case of an error, we terminate the program using . After obtaining the full path of the executable, we use the function to extract the directory containing the file.When executing the above program, it will print the directory of the currently running executable. This is very useful for many applications that need to locate resource files relative to the executable, such as configuration files and templates.