Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@WestleyK WestleyK commented Nov 14, 2018

👍 Thanks!

@whtiehack

This comment has been minimized.

Copy link

@whtiehack whtiehack commented Dec 14, 2018

+1 Thanks!

@XuDafang

This comment has been minimized.

Copy link

@XuDafang XuDafang commented Jan 31, 2019

Right !

@javiercbgr

This comment has been minimized.

Copy link

@javiercbgr javiercbgr commented Feb 4, 2019

Awesome, thanks!

@vinchauhan

This comment has been minimized.

Copy link

@vinchauhan vinchauhan commented Jun 5, 2019

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

This comment has been minimized.

Copy link

@webern webern commented Jul 28, 2019

👍

@stalko

This comment has been minimized.

Copy link

@stalko stalko commented Sep 13, 2019

👍🏿

@sh0seo

This comment has been minimized.

Copy link

@sh0seo sh0seo commented Oct 17, 2019

👍

@mzz2017

This comment has been minimized.

Copy link

@mzz2017 mzz2017 commented Oct 24, 2019

image

@vc1492a

This comment has been minimized.

Copy link

@vc1492a vc1492a commented Nov 8, 2019

👍

@pojntfx

This comment has been minimized.

Copy link

@pojntfx pojntfx commented Nov 26, 2019

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

@uurtech

This comment has been minimized.

Copy link

@uurtech uurtech commented Dec 7, 2019

thank you

@aseem2625

This comment has been minimized.

Copy link

@aseem2625 aseem2625 commented Jan 23, 2020

👍

@jasontconnell

This comment has been minimized.

Copy link

@jasontconnell jasontconnell commented May 6, 2020

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

This comment has been minimized.

Copy link

@vinchauhan vinchauhan commented May 6, 2020

@jasontconnell - you are right.

@wyfSunflower

This comment has been minimized.

Copy link

@wyfSunflower wyfSunflower commented Jun 15, 2020

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

@wule61

This comment has been minimized.

Copy link

@wule61 wule61 commented Oct 16, 2020

牛逼

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