我正在编写输出其他Go代码的Go代码。
我想知道是否有一种方法可以在完成编写的代码中调用gofmt工具来格式化代码。
我找到的关于gofmt的文档,例如官方文档,都是关于如何从命令行使用gofmt,但我想从Go代码本身中调用它。
示例:
我想知道是否有一种方法可以在完成编写的代码中调用gofmt工具来格式化代码。
我找到的关于gofmt的文档,例如官方文档,都是关于如何从命令行使用gofmt,但我想从Go代码本身中调用它。
示例:
func WriteToFile(content string) {
file, err := os.Create("../output/myFile.go")
if err != nil {
log.Fatal("Cannot create file", err)
}
defer file.Close()
fmt.Fprint(file, content)
//Insert code to call gofmt to automatically format myFile.go here
}
非常感谢您的时间和智慧。