Skip to content

Instantly share code, notes, and snippets.

@arxdsilva
Last active January 6, 2025 19:29
Show Gist options
  • Select an option

  • Save arxdsilva/4f73d6b89c9eac93d4ac887521121120 to your computer and use it in GitHub Desktop.

Select an option

Save arxdsilva/4f73d6b89c9eac93d4ac887521121120 to your computer and use it in GitHub Desktop.
How to get the current working directory in golang
package main
// More info on Getwd()
// https://golang.org/src/os/getwd.go
//
import(
"os"
"fmt"
"log"
)
func main() {
dir, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
fmt.Println(dir)
}
@WestleyK

Copy link
Copy Markdown

👍 Thanks!

@whtiehack

Copy link
Copy Markdown

+1 Thanks!

@XuDafang

Copy link
Copy Markdown

Right !

@reivash

reivash commented Feb 4, 2019

Copy link
Copy Markdown

Awesome, thanks!

@vinchauhan

vinchauhan commented Jun 5, 2019

Copy link
Copy Markdown

If you would like to find the current directory name - then you can extend it further.

// More info on Getwd()
// https://golang.org/src/os/getwd.go
// 
import(
  "os" 
  "fmt"
  "log"
)

func main() {
  dir, err := os.Getwd()
	if err != nil {
		log.Fatal(err)
	}
  fmt.Println(dir)
  var ss [] string
  if runtime.GOOS == "windows" {
		ss = strings.Split(dir, "\\")
	} else {
		ss = strings.Split(dir, "/")
	}

	currentDirName:= ss[len(ss)-1]

	fmt.Println("Current Directory Name: ", currentDirName)
}

@webern

webern commented Jul 28, 2019

Copy link
Copy Markdown

👍

@stalko

stalko commented Sep 13, 2019

Copy link
Copy Markdown

👍🏿

@sh0seo

sh0seo commented Oct 17, 2019

Copy link
Copy Markdown

👍

@mzz2017

mzz2017 commented Oct 24, 2019

Copy link
Copy Markdown

image

@vc1492a

vc1492a commented Nov 8, 2019

Copy link
Copy Markdown

👍

@pojntfx

pojntfx commented Nov 26, 2019

Copy link
Copy Markdown

I was looking for something like os.Pwd() - found this. Thanks a lot!

@aseem2625

Copy link
Copy Markdown

👍

@jasontconnell

Copy link
Copy Markdown

If you would like to find the current directory name - then you can extend it further.

// More info on Getwd()
// https://golang.org/src/os/getwd.go
// 
import(
  "os" 
  "fmt"
  "log"
)

func main() {
  dir, err := os.Getwd()
	if err != nil {
		log.Fatal(err)
	}
  fmt.Println(dir)
  var ss [] string
  if runtime.GOOS == "windows" {
		ss = strings.Split(dir, "\\")
	} else {
		ss = strings.Split(dir, "/")
	}

	currentDirName:= ss[len(ss)-1]

	fmt.Println("Current Directory Name: ", currentDirName)
}

path/filepath does this for you, hopefully no one has this code in their programs

https://golang.org/pkg/path/filepath/

@vinchauhan

Copy link
Copy Markdown

@jasontconnell - you are right.

@wyfSunflower

Copy link
Copy Markdown

Could you please tell me how to get directory of specified windows process ?not only current process but also some else.For example cmd.exe

@laredoer

Copy link
Copy Markdown

牛逼

@limjinyung

Copy link
Copy Markdown

Thanks 👍

@ganesh-getweave

Copy link
Copy Markdown

@m-a-rahal-os

Copy link
Copy Markdown

THANK YOU !
I've wasted so much time trying to figure out how to find directory of the project (to locate config file correctly), this should be the first answer on stack overflow or something ... thanks again for sharing

@Yegorov

Yegorov commented Jan 6, 2025

Copy link
Copy Markdown

If you would like to find the current directory name - then you can extend it further.

// More info on Getwd()
// https://golang.org/src/os/getwd.go
// 
import(
  "os" 
  "fmt"
  "log"
)

func main() {
  dir, err := os.Getwd()
	if err != nil {
		log.Fatal(err)
	}
  fmt.Println(dir)
  var ss [] string
  if runtime.GOOS == "windows" {
		ss = strings.Split(dir, "\\")
	} else {
		ss = strings.Split(dir, "/")
	}

	currentDirName:= ss[len(ss)-1]

	fmt.Println("Current Directory Name: ", currentDirName)
}

For split path it is probably better to use os.PathSeparator constant

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment