10.1. 命令行工具

  Name         Synopsis
  5a        5a is a version of the Plan 9 assembler.
  5c        5c is a version of the Plan 9 C compiler.
  5g        5g is the version of the gc compiler for the ARM. The $GOARCH for these tools is arm.
  5l        5l is a modified version of the Plan 9 linker.
  6a        6a is a version of the Plan 9 assembler.
  6c        6c is a version of the Plan 9 C compiler.
  6g        6g is the version of the gc compiler for the x86-64.
  6l        6l is a modified version of the Plan 9 linker.
  8a        8a is a version of the Plan 9 assembler.
  8c        8c is a version of the Plan 9 C compiler.
  8g        8g is the version of the gc compiler for the x86.
  8l        8l is a modified version of the Plan 9 linker.
  cc        This directory contains the portable section of the Plan 9 C compilers.
  cgo        Cgo enables the creation of Go packages that call C code.
  cov        Cov is a rudimentary code coverage tool.
  ebnflint    Ebnflint verifies that EBNF productions are consistent and gramatically correct.
  gc        Gc is the generic label for the family of Go compilers that function as part of the (modified) Plan 9 tool chain.
  godefs    Godefs is a bootstrapping tool for porting the Go runtime to new systems.
  godoc        Godoc extracts and generates documentation for Go programs.
  gofmt        Gofmt Go程序格式化.
  goinstall    Goinstall 尝试自动安装包的工具。
  gomake    gomake是针对go语言对GNU make命令的简单包装。
  gopack    Gopack is a variant of the Plan 9 ar tool.
  gotest    Gotest is an automated testing tool for Go packages.
  goyacc    Goyacc is a version of yacc for Go.
  hgpatch    Hgpatch applies a patch to the local Mercurial repository.
  ld        This directory contains the portable section of the Plan 9 C linkers.
  nm        Nm is a version of the Plan 9 nm command.
  prof        Prof is a rudimentary real-time profiler.

10.1.1. 8g

  8g is the version of the gc compiler for the x86.
  The $GOARCH for these tools is 386.

  It reads .go files and outputs .8 files. The flags are documented in ../gc/doc.go.

  There is no instruction optimizer, so the -N flag is a no-op.




  用法: 8g [flags] file.go...

    -I DIR search for packages in DIR 指定包的查找路径
    -d print declarations 打印声明
    -e no limit on number of errors printed 打印全部的错误
    -f print stack frame structure 打印栈帧结构
    -h panic on an error 遇到错误停止
    -o file specify output file 指定输出文件名
    -S print the assembly language 打印编译后的汇编代码
    -V print the compiler version 打印编译器版本
    -u disable package unsafe 禁用unsafe包
    -w print the parse tree after typing 打印分析树
    -x print lex tokens 打印词法分析结果

10.1.2. 8l

  8l is a modified version of the Plan 9 linker.  The original is documented at

  8l是Plan 9系统连接器的修改版。文档在:


  Its target architecture is the x86, referred to by these tools for historical reasons as 386.
  It reads files in .8 format generated by 8g, 8c, and 8a and emits
  a binary called 8.out by default.

  它的输入是8g, 8c,和8a生成的“.8”格式文件,然后默认输出8.out文件。

  Major changes include:
      - support for ELF and Mach-O binary files
      - support for segmented stacks (this feature is implemented here, not in the compilers).

      - 支持ELF和Mach-O格式的二进制文件 
      - 支持分段的栈(该特性不是在编译器实现,是在这里)

  Original options are listed in the link above.


  Options new in this version:


      Elide the dynamic linking header.  With this option, the binary
      is statically linked and does not refer to dynld.  Without this option
      (the default), the binary's contents are identical but it is loaded with dynld.
      Write Apple Mach-O binaries (default when $GOOS is darwin)
      生成Apple Mach-O格式文件($GOOS为darwin)
      Write Linux ELF binaries (default when $GOOS is linux)
  -L dir1,dir2,..
      Search for libraries (package files) in the comma-separated list of directories.
      The default is the single location $GOROOT/pkg/$GOOS_386.
  -r dir1:dir2:...
      Set the dynamic linker search path when using ELF.
      Print the linker version.

10.1.3. 8a

  8a is a version of the Plan 9 assembler.  The original is documented at

  8a是Plan 9的汇编器,文档在


  Its target architecture is the x86, referred to by these tools for historical reasons as 386.


10.1.4. gomake

  The gomake command runs GNU make with an appropriate environment
  for using the conventional Go makefiles.  If $GOROOT is already
  set in the environment, running gomake is exactly the same
  as running make (or, on BSD systems, running gmake).

  gomake是针对go语言对GNU make命令的简单包装。


  用法: gomake [ 目标 ... ]


      all (默认)
          build the package or command, but do not install it.

          build and install the package or command

          run the tests (packages only)

          run benchmarks (packages only)

          remove object files from the current directory

          make clean and remove the installed package or command

  查看 http://golang.org/doc/code.html 页面,获取关于makefiles的详细信息。

10.1.5. cgo


  Cgo enables the creation of Go packages that call C code.


  Usage: cgo [compiler options] file.go

  The compiler options are passed through uninterpreted when
  invoking gcc to compile the C parts of the package.


  The input file.go is a syntactically valid Go source file that imports
  the pseudo-package "C" and then refers to types such as C.size_t,
  variables such as C.stdout, or functions such as C.putchar.

  的内容,如 C.size_t、C.stdout、C.putchar。

  If the import of "C" is immediately preceded by a comment, that
  comment is used as a header when compiling the C parts of
  the package.  For example:


      // #include <stdio.h>
      // #include <errno.h>
      import "C"

  C identifiers or field names that are keywords in Go can be
  accessed by prefixing them with an underscore: if x points at
  a C struct with a field named "type", x._type accesses the field.

  The standard C numeric types are available under the names
  C.char, C.schar (signed char), C.uchar (unsigned char),
  C.short, C.ushort (unsigned short), C.int, C.uint (unsigned int),
  C.long, C.ulong (unsigned long), C.longlong (long long),
  C.ulonglong (unsigned long long), C.float, C.double.

  C.char, C.schar (signed char), C.uchar (unsigned char),
  C.short, C.ushort (unsigned short), C.int, C.uint (unsigned int),
  C.long, C.ulong (unsigned long), C.longlong (long long),
  C.ulonglong (unsigned long long), C.float, C.double.

  To access a struct, union, or enum type directly, prefix it with
  struct_, union_, or enum_, as in C.struct_stat.

  如果是struct, union, 或 enum 类型,添加前缀struct_, union_, or enum_,
  例如: C.struct_stat.

  Any C function that returns a value may be called in a multiple
  assignment context to retrieve both the return value and the
  C errno variable as an os.Error.  For example:


      n, err := C.atoi("abc")

  In C, a function argument written as a fixed size array
  actually requires a pointer to the first element of the array.
  C compilers are aware of this calling convention and adjust
  the call accordingly, but Go cannot.  In Go, you must pass
  the pointer to the first element explicitly: C.f(&x[0]).


  Cgo transforms the input file into four output files: two Go source
  files, a C file for 6c (or 8c or 5c), and a C file for gcc.


  The standard package makefile rules in Make.pkg automate the
  process of using cgo.  See $GOROOT/misc/cgo/stdio and
  $GOROOT/misc/cgo/gmp for examples.


  Cgo does not yet work with gccgo.


10.1.6. gotest

  Gotest is an automated testing tool for Go packages.


  Normally a Go package is compiled without its test files.  Gotest
  is a simple script that recompiles the package along with any files
  named *_test.go.  Functions in the test sources named TestXXX
  (where XXX is any alphanumeric string starting with an upper case
  letter) will be run when the binary is executed.  Gotest requires
  that the package have a standard package Makefile, one that
  includes go/src/Make.pkg.


  The test functions are run in the order they appear in the source.
  They should have signature


      func TestXXX(t *testing.T) { ... }

  Benchmark functions can be written as well; they will be run only
  when the -benchmarks flag is provided.  Benchmarks should have

  基准测试也已经支持,只要在命令行增加 -benchmarks 选项。基准测试函数的类型:

      func BenchmarkXXX(b *testing.B) { ... }

  See the documentation of the testing package for more information.


  By default, gotest needs no arguments.  It compiles all the .go files
  in the directory, including tests, and runs the tests.  If file names
  are given, only those test files are added to the package.
  (The non-test files are always compiled.)


  The package is built in a special subdirectory so it does not
  interfere with the non-test installation.


      gotest [pkg_test.go ...]

  The resulting binary, called (for amd64) 6.out, has a couple of


      6.out [-v] [-match pattern] [-benchmarks pattern]

  The -v flag causes the tests to be logged as they run.  The -match
  flag causes only those tests whose names match the regular expression
  pattern to be run. By default all tests are run silently.  If all
  the specified test pass, 6.out prints PASS and exits with a 0 exit
  code.  If any tests fail, it prints FAIL and exits with a non-zero
  code.  The -benchmarks flag is analogous to the -match flag, but
  applies to benchmarks.  No benchmarks run by default.


10.1.7. Goyacc

  Goyacc is a version of yacc for Go.
  It is written in Go and generates parsers written in Go.

  It is largely transliterated from the Inferno version written in Limbo
  which in turn was largely transliterated from the Plan 9 version
  written in C and documented at


  Yacc adepts will have no trouble adapting to this form of the tool.

  The file units.y in this directory is a yacc grammar for a version of
  the Unix tool units, also written in Go and largely transliterated
  from the Plan 9 C version.

  The generated parser is reentrant. Parse expects to be given an
  argument that conforms to the following interface:

      type yyLexer interface {
          Lex(lval *yySymType) int
          Error(e string)

  Lex should return the token identifier, and place other token
  information in lval (which replaces the usual yylval).
  Error is equivalent to yyerror in the original yacc.

  Code inside the parser may refer to the variable yylex
  which holds the yyLexer passed to Parse.

10.1.8. gopack

  Gopack is a variant of the Plan 9 ar tool.  The original is documented at


  It adds a special Go-specific section __.PKGDEF that collects all the
  Go type information from the files in the archive; that section is
  used by the compiler when importing the package during compilation.

  Usage: gopack [uvnbailo][mrxtdpq] archive files ...

  The new option 'g' causes gopack to maintain the __.PKGDEF section
  as files are added to the archive.

10.1.9. gofmt

  gofmt 程序格式化.

  Without an explicit path, it processes the standard input. Given a file, 
  it operates on that file; given a directory, it operates on all .go files in that directory, 
  recursively. (Files starting with a period are ignored.)

  指定了路径就递归指定路径下面的所有.go文件。(Files starting with a period are ignored.)


  gofmt [flags] [path ...]


      just list files whose formatting differs from gofmt's; generate no other output
  unless -w is also set.
      只列出gofmt需要格式化的文件,不对文件做任何操作,除非使用 -w
  -r rule
      apply the rewrite rule to the source before reformatting.
      try to simplify code (after applying the rewrite rule, if any).
      if set, overwrite each input file with its output.
      align with spaces instead of tabs.
      indent with tabs independent of -spaces.
      tab width in spaces.


      print parse trace.
      print AST (before rewrites).
      打印AST (在重写之前)
      print comments; if false, all comments are elided from the output.
      打印注释,如果是假(false), 所有的注释信息不做处理

  The rewrite rule specified with the -r flag must be a string of the form:
  选项 -r 的重写规则必须遵循这个模式:

  pattern -> replacement

  Both pattern and replacement must be valid Go expressions. In the pattern, 
  single-character lowercase identifiers serve as wildcards matching arbitrary sub-expressions; 
  those expressions will be substituted for the same identifiers in the replacement.
  pattern和replacement必须是有效的Go语法。Pattern 单字符小写标识符作为通配符匹配任意表达式,


  To check files for unnecessary parentheses:

  gofmt -r '(a) -> a' -l *.go

  To remove the parentheses:

  gofmt -r '(a) -> a' -w *.go

  To convert the package tree from explicit slice upper bounds to implicit ones:

  gofmt -r 'α[β:len(α)] -> α[β:]' -w $GOROOT/src/pkg


  The implementation of -r is a bit slow.
  选项 -r 的实现方式效率有些低

10.1.10. goinstall

  Goinstall is an experiment in automatic package installation.
  It installs packages, possibly downloading them from the internet.
  It maintains a list of public Go packages at http://godashboard.appspot.com/package.

      goinstall [flags] importpath...
      goinstall [flags] -a

  Flags and default settings:
          -a=false          install all previously installed packages
      -dashboard=true   tally public packages on godashboard.appspot.com
      -log=true         log installed packages to $GOROOT/goinstall.log for use by -a
      -u=false          update already-downloaded packages
      -v=false          verbose operation

  Goinstall installs each of the packages identified on the command line.  It
  installs a package's prerequisites before trying to install the package
  itself. Unless -log=false is specified, goinstall logs the import path of each
  installed package to $GOROOT/goinstall.log for use by goinstall -a.

  If the -a flag is given, goinstall reinstalls all previously installed
  packages, reading the list from $GOROOT/goinstall.log.  After updating to a
  new Go release, which deletes all package binaries, running

      goinstall -a

  will recompile and reinstall goinstalled packages.

  Another common idiom is to use

      goinstall -a -u

  to update, recompile, and reinstall all goinstalled packages.

  The source code for a package with import path foo/bar is expected
  to be in the directory $GOROOT/src/pkg/foo/bar/.  If the import
  path refers to a code hosting site, goinstall will download the code
  if necessary.  The recognized code hosting sites are:

      BitBucket (Mercurial)

          import "bitbucket.org/user/project"
          import "bitbucket.org/user/project/sub/directory"

      GitHub (Git)

          import "github.com/user/project.git"
          import "github.com/user/project.git/sub/directory"

      Google Code Project Hosting (Mercurial, Subversion)

          import "project.googlecode.com/hg"
          import "project.googlecode.com/hg/sub/directory"

          import "project.googlecode.com/svn/trunk"
          import "project.googlecode.com/svn/trunk/sub/directory"


          import "launchpad.net/project"
          import "launchpad.net/project/series"
          import "launchpad.net/project/series/sub/directory"

          import "launchpad.net/~user/project/branch"
          import "launchpad.net/~user/project/branch/sub/directory"

  If the destination directory (e.g., $GOROOT/src/pkg/bitbucket.org/user/project)
  already exists and contains an appropriate checkout, goinstall will not
  attempt to fetch updates.  The -u flag changes this behavior,
  causing goinstall to update all remote packages encountered during
  the installation.

  When downloading or updating, goinstall first looks for a tag or branch
  named "release".  If there is one, it uses that version of the code.
  Otherwise it uses the default version selected by the version control
  system, typically HEAD for git, tip for Mercurial.

  After a successful download and installation of a publicly accessible
  remote package, goinstall reports the installation to godashboard.appspot.com,
  which increments a count associated with the package and the time
  of its most recent installation.  This mechanism powers the package list
  at http://godashboard.appspot.com/package, allowing Go programmers
  to learn about popular packages that might be worth looking at.
  The -dashboard=false flag disables this reporting.

  By default, goinstall prints output only when it encounters an error.
  The -v flag causes goinstall to print information about packages
  being considered and installed.

  Goinstall does not attempt to be a replacement for make.
  Instead, it invokes "make install" after locating the package sources.
  For local packages without a Makefile and all remote packages,
  goinstall creates and uses a temporary Makefile constructed from
  the import path and the list of Go files in the package.