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 vc6.0中想运行代码的时候总是提示无法打开文件是怎么回事
  • ¥25 关于##爬虫##的问题,如何解决?:
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题