首页 > 解决方案 > Golang 中是否有任何命令行来检查我的 go 源代码的语法错误并将错误写入文件?

问题描述

package main

import "fmt"

func plus(a int, b int) int {
    return a + b
}
}
func plusPlus(a, b, c int) int {
    return a + b + c
}

func main() {
    res := plus(1, 2)
    fmt.Println("1+2 =", res)

    res = plusPlus(1, 2, 3)
    fmt.Println("1+2+3 =", res)
}

这是我的 Go 源代码。 gofmt -e my_file.go正在工作但我无法将错误写入文本文件。

标签: go

解决方案


gofmt将错误输出到标准错误。您可以简单地将标准错误流重定向gofmt到 unix 系统上的文件,如下所示:

gofmt -e my_file.go 2>a.txt

检查它:

more a.txt

输出:

my_file.go:8:1: expected declaration, found '}'

推荐阅读