I am using golang revel web framework and
I am trying to create a sqlite
db in the current working directory.
model.go
func New(dbName string,table string) *Db {
_,filename,_,_ := runtime.Caller(1)
db , err := sql.Open("sqlite3",path.Join(path.Dir(filename),dbName))
if err != nil {
log.Fatal(err)
}
err = db.Ping()
if err != nil {
log.Panic(err)
}
database := &Db{Database:db}
_,err = db.Exec("create table %s" +
"( id integer primary key, " +
"name varchar(100),"+
"email varchar(100),"+
"branch varchar(100),"+
"help varchar(100)",)
if err != nil {
log.Fatal(err)
}
}
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
?