Golang : Get current file path of a file or executable

Tags : golang file-path

Getting a file path in Golang is not that straight forward with just a single function call. In this tutorial, we will learn how to get the current file path of a file with filepath.Abs.


 package main

 import (

 func main () {

 filename := os.Args[1] // get command line first parameter

 filedirectory := filepath.Dir(filename)

 thepath, err := filepath.Abs(filedirectory)

 if err != nil {


Executing > go run getfilepath.go somefile will return the current path of the somefile

For finding out the executable file path. In most cases, os.Getwd should do the job.

You can use the above code as well to find the current path of the executing program. Just change the line

filename := os.Args[1] // get command line first parameter


filename := os.Args[0] // use own filename

Hope this tutorial can be useful to you.

References :



  See also : Golang : File path independent of Operating System

Tags : golang file-path

By Adam Ng

IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.