ADVERTISEMENT
ADVERTISEMENT

How to get the directory of the currently running file in Golang?

Here, we have to get the directory of the currently running file.
Submitted by IncludeHelp, on December 05, 2021

To get the directory of the currently running file – We use the Executable() function of the os package. This function accepts nothing and returns a string containing the path name for the executable that started the current process. and return an error if any. And, then Dir() function of the path/filepath package.

Syntax:

func Executable() (string, error)

Program:

package main

import (
	"fmt"
	"os"
	"path/filepath"
)

func main() {
	// Getting the path name for the executable
	// that started the current process.
	pathExecutable, err := os.Executable()
	if err != nil {
		panic(err)
	}
	// Getting the directory path/name
	dirPathExecutable := filepath.Dir(pathExecutable)

	fmt.Println("Directory of the currently running file...")
	fmt.Println(dirPathExecutable)
}

Output:

Directory of the currently running file...
/tmpfs

Golang FAQ »

ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.