How can I get a file in a Go web project for testing and production?

huangapple go评论73阅读模式
英文:

How can I get a file in a Go web project for testing and production?

问题

我目前正在进行一个网络项目,我们在后端使用Go(与martini一起)。其中包含一个反向地理编码器,将坐标映射到城市名称。为此,反向地理编码器必须读取一个 cities.csv 文件。

结构如下:

handlers/city/create.go
services/geo/reverse.go
services/geo/cities.csv
main.go

现在,main.go 用于启动Web服务。处理程序 handlers/city/create.go 使用 services/geo/reverse.go 来获取带有 cities.csv 的城市。

问题是如何获取 cities.csv

我尝试过的方法

纯文件名

然而,当我只使用 csvFilename := "cities.csv" 时:

  • 测试可以正常工作
  • 处理程序无法正常工作,因为Go假定文件路径为 /home/me/go/src/github.com/githubuser/backend/cities.csv

调整后的文件名

当我将文件名调整为相对于根目录的路径(csvFilename := "services/geocalc/cities.csv")时,测试失败。它们假定文件路径为 /home/me/GitHub/go/src/github.com/githubuser/backend/services/geocalc/services/geocalc/city-names-geocoordinates.csv

args[0]

这种方法也不起作用:

filename := filepath.Dir(os.Args[0])
filedirectory := filepath.Dir(filename)
csvFilename, _ := filepath.Abs(path.Join(filedirectory, "cities.csv"))

现在,测试失败,文件路径为 /tmp/go-build210484207/github.com/githubuser/logbook-backend/services/geocalc/cities.csv

runtime caller

_, filename, _, _ := runtime.Caller(1)
filedirectory := filepath.Dir(filename)
csvFilename, _ := filepath.Abs(path.Join(filedirectory, "cities.csv"))

这对于测试来说是有效的,但在“生产环境”中(使用http查询进行测试)它假定文件路径为 /home/me/GitHub/go/src/github.com/githubuser/backend/handlers/packets/cities.csv

os.Getwd()

版本1

filedirectory, _ := os.Getwd()
csvFilename, _ := filepath.Abs(path.Join(filedirectory, "cities.csv"))

在生产环境中失败,文件路径为 /home/me/GitHub/go/src/github.com/githubuser/logbook-backend/cities.csv

版本2

filedirectory, _ := os.Getwd()
csvFilename, _ := filepath.Abs(path.Join(filedirectory, "services/geo/cities.csv"))

在测试中失败,文件路径为 /home/me/GitHub/go/src/github.com/githubuser/logbook-backend/services/geo/services/geo/cities.csv

英文:

I am currently working on a web project where we use Go (with martini) a backend. It contains a reverse-geocoder which maps coordinates to city names. To do so, the reverse-geocoder has to read a cities.csv.

The structure is

handlers/city/create.go
services/geo/reverse.go
services/geo/cities.csv
main.go

Now the main.go is started to start the web service. The handler handlers/city/create.go makes use of services/geo/reverse.go to get
the city with cities.csv.

The problem is to get the cities.csv.

What I've tried

plain filename

However, when I only use csvFilename := "cities.csv":

  • the tests work
  • the handler doesn't work as go assumes /home/me/go/src/github.com/githubuser/backend/cities.csv

adjusted filename

When I adjust the filename to be relative to the root (csvFilename := "services/geocalc/cities.csv"), the tests fail. They assume /home/me/GitHub/go/src/github.com/githubuser/backend/services/geocalc/services/geocalc/city-names-geocoordinates.csv.

args[0]

This doesn't work either:

filename := filepath.Dir(os.Args[0])
filedirectory := filepath.Dir(filename)
csvFilename, _ := filepath.Abs(path.Join(filedirectory, "cities.csv"))

Now the tests fail with /tmp/go-build210484207/github.com/githubuser/logbook-backend/services/geocalc/cities.csv

runntime caller

_, filename, _, _ := runtime.Caller(1)
filedirectory := filepath.Dir(filename)
csvFilename, _ := filepath.Abs(path.Join(filedirectory, "cities.csv"))

works for the tests, but in "production" (testing with http-queries) it assumes /home/me/GitHub/go/src/github.com/githubuser/backend/handlers/packets/cities.csv

os.Getwd()

Version 1

filedirectory, _ := os.Getwd()
csvFilename, _ := filepath.Abs(path.Join(filedirectory, "cities.csv"))

fails in production with /home/me/GitHub/go/src/github.com/githubuser/logbook-backend/cities.csv.

Version 2

filedirectory, _ := os.Getwd()
csvFilename, _ := filepath.Abs(path.Join(filedirectory, "services/geo/cities.csv"))

Fails in the test with /home/me/GitHub/go/src/github.com/githubuser/logbook-backend/services/geo/services/geo/cities.csv

答案1

得分: 0

我刚刚意识到我可以使用GOPATH环境变量。它始终被设置(参见The GOPATH environment variable),因此不需要额外的工作:

filedirectory := os.Getenv("GOPATH")
csvFilename, _ := filepath.Abs(path.Join(filedirectory, "src/github.com/gituser/backend/services/geo/cities.csv"))
csvfile, err := os.Open(csvFilename)
英文:

I just realized that I can use the GOPATH environment variable. It is always set (see The GOPATH environment variable) and therefore no additional work:

filedirectory := os.Getenv("GOPATH")
csvFilename, _ := filepath.Abs(path.Join(filedirectory, "src/github.com/gituser/backend/services/geo/cities.csv"))
csvfile, err := os.Open(csvFilename)

huangapple
  • 本文由 发表于 2015年3月29日 04:13:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/29322065.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定