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

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

当我调用以下代码时, p>

  func GetCurrentDirectory(  )字符串{
目录,错误:= filepath.Abs​​(filepath.Dir(os.Args [0]))
  code>  pre> 
 
 

它返回: p>

/ private / var / folders / cg / mwzlhrjs5y55ny553g6xz9tr0000gn / T p> blockquote>

当然,这是一条临时路径,我 路径是我的当前目录,而不是临时目录。 p>

  dir,err:= filepath.Abs​​(“”)
  code>  pre> 
  
 

可以帮助我,但我必须判断是否在goland中处于运行,调试模式 p> div>

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.