os.Open
报错原因:Open不是当前目录下查找,
项目取的是项目根目录,不是执行目录:证明如下:
exPath,_ := os.Getwd()
fmt.Println("expath",exPath)
file, err := os.Open(exPath+"/"+filename)
解决方法:
换成绝对路径:
1.
设置output directory 和Working directory:
都为run目录,打开文件就会冲这里找:
方法2:换成绝对路径:
ex, err := os.Executable()
if err != nil {
panic(err)
}
exPath := filepath.Dir(ex)
file, err := os.Open(exPath+"/"+filename)
if err != nil {
return nil, 0 ,0
}
defer file.Close()
stats, statsErr := file.Stat()
if statsErr != nil {
return nil, 0,0
}
var totalSize int64 = stats.Size()
bytes := make([]byte, offset)
retSize,_ := file.ReadAt(bytes, start)
return bytes, retSize, totalSize