I'm trying to a read a file (a.go) present in a package into another file (b.go) which is also present in the same package. I used os.Open() method but it is working only when i give the complete system path of the file(ex: home/bin/xyz/a.go).
But, I want the file (a.go) to be read into (b.go) without using the entire system path. Can anyone please help?
Folder
-- a.go
-- b.go
//Inside b.go we have a function//
func (xyz){
x:= os.Open("--path to a.go--") // This path shouldn't be the system path
}
You can get the path of the current file by using this technique.
_, srcpath, _, _ := runtime.Caller(0)
dirpath := filepath.Dir(srcpath)
err, fp := os.Open(filepath.Join(dirpath, "a.go"))
if err != nil {
// err
}
...
fp.Close()