The problem is I can't reach the file relative to the package path where it is actual used. Let's consider the example. I have the following structure:
~/go/src/github.com/user/dbms
data/
database.db
dbms.go
~/projects/myproj/bin
main.go
dbms.go:
package dbms
import (
"os"
"fmt"
"path/filepath"
)
type dbms struct {
filepath string
}
func New() *dbms {
return &dbms{filepath: "./data/database.db"}
}
func (d *dbms) Run() {
fmt.Println(filepath.Abs(d.Path))
// output: /home/timur/projects/myproj/bin/data
// I need: /home/timur/go/src/github.com/user/dbms/data
file, err := os.OpenFile(d.filepath, os.O_RDWR, 0666)
// error
}
main.go:
package main
import (
"github.com/user/dbms"
)
func main() {
db := dbms.New()
db.Run()
}
As you can see dbms.Path
resolves path relative to the entry point and not package itself. What do I wrong?