我目前正在开发一个 Web 项目,在该项目中我们使用 Go(带马提尼)作为后端。它包含一个反向地理编码器,可将坐标映射到城市名称。为此,反向地理编码器必须读取cities.csv.结构是handlers/city/create.goservices/geo/reverse.goservices/geo/cities.csvmain.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.参数[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运行时调用者_, 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.csvos.Getwd()版本 1filedirectory, _ := os.Getwd()csvFilename, _ := filepath.Abs(path.Join(filedirectory, "cities.csv"))生产失败/home/me/GitHub/go/src/github.com/githubuser/logbook-backend/cities.csv。版本 2filedirectory, _ := 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
1 回答
- 1 回答
- 0 关注
- 170 浏览
添加回答
举报
0/150
提交
取消