如何在Revel中获取当前文件的位置

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

how to get the location of the current file in revel

问题

我正在使用golang revel web框架,并尝试在当前工作目录中创建一个sqlite数据库。

model.go

  1. func New(dbName string, table string) *Db {
  2. _, filename, _, _ := runtime.Caller(1)
  3. db, err := sql.Open("sqlite3", path.Join(path.Dir(filename), dbName))
  4. if err != nil {
  5. log.Fatal(err)
  6. }
  7. err = db.Ping()
  8. if err != nil {
  9. log.Panic(err)
  10. }
  11. database := &Db{Database: db}
  12. _, err = db.Exec("create table %s" +
  13. "( id integer primary key, " +
  14. "name varchar(100)," +
  15. "email varchar(100)," +
  16. "branch varchar(100)," +
  17. "help varchar(100))")
  18. if err != nil {
  19. log.Fatal(err)
  20. }
  21. }

我已经编写了一个测试,只是调用了这个函数。

每当我使用revel test运行测试,或者通过访问localhost:9000/@tests运行测试时,函数会引发Panic,并显示错误消息无法打开数据库文件

发生这种情况的原因是runtime.Caller(1)返回的filename/usr/local/go/src/runtime/asm_amd64.s,程序对其没有权限。

即使我直接写入./foo.db,错误仍然显示。

我尝试了os.Getwd(),它返回空字符串。

我还尝试了filepath.Abs(filepath.Dir(os.Args[0])),但它返回的是/home/girish/GoProjects/bin/revel.d,这是revel二进制文件的位置。

那么找到model.go所在目录的最佳方法是什么?

英文:

I am using golang revel web framework and
I am trying to create a sqlite db in the current working directory.

model.go

  1. func New(dbName string,table string) *Db {
  2. _,filename,_,_ := runtime.Caller(1)
  3. db , err := sql.Open("sqlite3",path.Join(path.Dir(filename),dbName))
  4. if err != nil {
  5. log.Fatal(err)
  6. }
  7. err = db.Ping()
  8. if err != nil {
  9. log.Panic(err)
  10. }
  11. database := &Db{Database:db}
  12. _,err = db.Exec("create table %s" +
  13. "( id integer primary key, " +
  14. "name varchar(100),"+
  15. "email varchar(100),"+
  16. "branch varchar(100),"+
  17. "help varchar(100)",)
  18. if err != nil {
  19. log.Fatal(err)
  20. }

}

I have a test in place which just calls this function.

whenever i run the test using revel test or by going to the localhost:9000/@tests, the function Panics and the error message is
cannot open the database file.

The reason that is happening is because the filename returned by runtime.Caller(1) is /usr/local/go/src/runtime/asm_amd64.s for which the program has no permission.

if i directly write ./foo.db, even then the error shows.

I tried os.Getwd() which return empty string.

I also tried filepath.Abs(filepath.Dir(os.Args[0]))
but that returned /home/girish/GoProjects/bin/revel.d which is the revel binary.

So whats the best way to find the directory of the model.go?

答案1

得分: 1

在运行时获取model.go文件的目录并没有意义,因为编译后的可执行文件可能位于完全不同的文件系统上。

您可能想要获取正在运行的可执行文件的目录:

  1. dir, err := filepath.Abs(filepath.Dir(os.Args[0]))

dir将是程序在运行时所在的文件夹。

英文:

It doesn't make sense to get the directory of the model.go file at runtime, because the compiled executable could be on a completely different filesystem.

You may want to get the directory of where the running executable was started from:

  1. dir, err := filepath.Abs(filepath.Dir(os.Args[0]))

dir will be the folder where the program lives at runtime.

huangapple
  • 本文由 发表于 2016年3月27日 01:05:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/36238206.html
匿名

发表评论

匿名网友

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

确定