-
-
Save arxdsilva/4f73d6b89c9eac93d4ac887521121120 to your computer and use it in GitHub Desktop.
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) | |
} |
+1 Thanks!
Right !
Awesome, thanks!
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)
}
👍
👍🏿
👍
👍
I was looking for something like os.Pwd()
- found this. Thanks a lot!
👍
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
@jasontconnell - you are right.
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
牛逼
Thanks 👍
for current dir name https://pkg.go.dev/path/filepath#Base
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
👍 Thanks!