I am a package whose only role is to provide a collection of dummy data read from files.
It looks like this:
func GetArrayOfSize(n int) []int {
f, _ := os.Open("./arrays.txt")
defer f.Close()
numbers := make([]int, 0)
scanner := bufio.NewScanner(f)
for scanner.Scan() {
s, _ := strconv.Atoi(scanner.Text())
numbers = append(numbers, s)
}
return numbers[0:n]
}
It works fine when I test it inside this package however whenever I call GetArrayOfSize
from another package I get a runtime error:
panic: runtime error: slice bounds out of range [recovered]
panic: runtime error: slice bounds out of range
I am guessing this error is due to the relative path: './arrays.txt
. How can I fetch the absolute path of my dummy
package to fix this?
Many thanks