Golang在运行和调试时无法获得正确的当前路径?

When I invoke the following code,

 func GetCurrentDirectory() string {
    dir, err := filepath.Abs(filepath.Dir(os.Args[0])) 

it returns:

/private/var/folders/cg/mwzlhrjs5y55ny553g6xz9tr0000gn/T

Absolutely, it is a temp path, I excepted the path is my current directory, not temporary directory.

dir, err := filepath.Abs("")

could help me,but I must judge whether in run、debug mode in goland or not

In order to fix this, go to Run | Edit Configuration... | <name of your configuration> and change the Working Directory property in order to change the working directory of the application or configure the Output directory in order to configure where the binary is created and run from (by default it's in the temporary directory of your OS).

I recommend adding a debug parameter.

package main

import (
    "flag"
    "fmt"
)

func main() {
    var debug bool
    flag.BoolVar(&debug, "d", false, "debug")
    flag.Parse()
    fmt.Println(debug)
}

Then pass -d it when you run the program from your IDE.