golang文件相对路径问题

  1. 目录结构
    2.
    2.具体代码:
	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()

你可能感兴趣的:(golang,开发语言,后端)