duanlian1320 2013-05-31 17:46
浏览 66
已采纳

Golang中有一个命令行工具只能检查我的源代码的语法吗?

For example there is a -c option in Ruby that checks syntax before running a code:

C:\>ruby --help
Usage: ruby [switches] [--] [programfile] [arguments]
-c              check syntax only

C:\>ruby -c C:\foouby\my_source_code.rb
Syntax OK

Is there a similar functionality in Go?

P.S. An example from Ruby is only because I know it in Ruby. Not because of trolling or something.

  • 写回答

6条回答 默认 最新

  • drw85135 2013-05-31 18:02
    关注

    You can use gofmt to check for syntax errors without actually building the project.

    gofmt -e my_file.go > /dev/null
    

    You can later use $? bash variable, return code 0 implies success, 2 means syntax check. /dev/null will eat the code, but the errors go to stderr

    The -e option is defined as:

    report all errors (not just the first 10 on different lines)


    gofmt --help
    
    usage: gofmt [flags] [path ...]
      -comments=true: print comments
      -cpuprofile="": write cpu profile to this file
      -d=false: display diffs instead of rewriting files
      -e=false: report all errors (not just the first 10 on different lines)
      -l=false: list files whose formatting differs from gofmt's
      -r="": rewrite rule (e.g., 'a[b:len(a)] -> a[b:]')
      -s=false: simplify code
      -tabs=true: indent with tabs
      -tabwidth=8: tab width
      -w=false: write result to (source) file instead of stdout
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题