English 中文(简体)
在戈里使用相对档案途径作为功能参数
原标题:Using relative file paths in Go as function parameters

让我说,我有这样多的结构:

src/
- main.go
data/
- example.csv
csv/
- parse.go
utils/
- mapFields.go

地图现场功能将投入和产出卷宗作为准点:

MapFields(csvInPath string, csvOutPath string) {...}

我希望能够利用相对档案途径从任何戈文档中标明这一地图现场功能。 例如,主编。 :

MapFields("../data/example.csv", "../data/output.csv")

我不能说,如何确保地图场开车,使用从电话功能的档案道路上铺设的道路,以及车上相对的道路。

我使用了。 在诺德.js执行相当于_dir name的解决办法的支流站,但没有成功。

我不胜感激。

问题回答

文件路径根据您目前的工作名录解决。

During development it is usually the root directory of your project. You can check what is your current directory using os.Getwd().

func main() {
    if workingDirectory, err := os.Getwd(); err != nil {
        panic(fmt.Sprintf("cennot get working direcory - %s", err))
    } else {
        fmt.Printf("Working direcory is %s", workingDirectory)
    }
}

For more flexibility consider using environment variable to indicate the root directory for your csv files. if environment variable is not set use filepath.Join(".", "data").

@JimB and @m-szalik write good answers and advices. That looks good enough to solve your problem. But I try to summarize the answers in my style.

Summary

你们需要把投入文件放在与汇编和执行的双亲档案工作目录一致的名录中,而不是放在你发展环境的目录上。

您可在以下档案等级中使用<代码>。 重要的是将执行你的双手。

// run main at ./src
➜  src ./main                

// file hierarchy
.
├── data
│   ├── example.csv
│   └── output.csv
└── src
    ├── main
    └── main.go

Definitions we have to know

简言之,relative path取决于 工作名录,其中your汇编并实施了双亲文件

Therefore, JimB said the compiled binary matters.

Absolute path

asolute or full path >在档案系统中指明了同一地点,而不论目前的工作名录如何。 为此,它必须包括根基。

Relative path

By contrast, a relative path starts from some given working directory, ...

https://en.wikipedia.org/wiki/Path_(computing)#Absolute_and_relative_paths

Working directory

在计算时,工艺目录()是等级档案系统的目录,如果与这一过程有动态关系的话。 有时称为目前的工作名录。

https://en.wikipedia.org/wiki/Working_directory

Simplified example

package main

import (
    "encoding/json"
    "fmt"
    "os"
)

type Human struct {
    Life string `json:"life"`
}

func main() {
    var human Human
    data, errRead := os.ReadFile("txt/input")
    errUnMarshall := json.Unmarshal(data, &human)

    fmt.Println(errRead)
    fmt.Println(errUnMarshall)
    fmt.Println(human)
}

This code reads input text from its relative path txt/ And its directories and files are like this.

.
└── src
    ├── main
    ├── main.go
    └── txt
        └── input

Case 1

I run main on src/. Then it works. The reason is main s working directory src/ and it corresponds with os.ReadFile("txt/input")

它照此印刷。

➜  src ./main                
<nil>
<nil>
{Life is hard}

Case 2

I run main on .. Then it fails. The reason is main s working directory . and it doesn t correspond with os.ReadFile("txt/input")

➜ . ./src/main 
open txt/input: no such file or directory
unexpected end of JSON input
{}

Etc

您可以发现,工作名录是您的双亲在终点站的名录。 重要的是该方案的指南。

If you re using IDE such as IntelliJ, then you can set the working directory at Run/Debug configuration. enter image description here





相关问题
Code snippet paths in GCC

Background: Keil C51 on a PC, currently moving to GCC (CrossPack-AVR) on an iMac. Since I write firmware for micro s I have a lot of driver source files etc. that I need to include with my programs,...

C#/.NET app doesn t recognize Environment Var Change (PATH)

In my C# app, I am programmatically installing an Oracle client if one is not present, which requires adding a dir to the PATH system environment variable. This all works fine, but it doesn t take ...

PHP absolute path to file URI

In order to refer to a local DTD when using PHP SimpleXML, I need to convert the absolute path into a file type URI. For example, convert /home/sitename/www/xml/example.dtd to file:///home/sitename/...

Check if files with absolute and relative path exists

Is there a way to check whether files (with either an absolute or relative path) exists? Im using PHP. I found a couple of method but either they only accept absolute or relative but not both. Thanks.

How to get file path of the file with the running-vba code

I want to get the folder path from the file who is running the vba-code. For example: the vba-code is in the file C:myfolderfile.accdb and I want the vba-code to return C:myfolder Is that ...

热门标签