- 目录结构
2.具体代码:
go
const dataFile = "../data/data.json"
_, fileName, _, _ := runtime.Caller(1)
dataPath := path.Join(path.Dir(fileName), dataFile)
fmt.Println(dataPath)
// open file
file, err := os.Open(dataPath)
if err != nil {
log.Fatalln(err)
}
// don't forgot close file handler
defer file.Close()