Featured image of post golang学习二:golang自带的工具

golang学习二:golang自带的工具

golang开发包自带的工具go.exe、godoc.exe、gofmt.exe

解压版Go语言安装包中自带工具

  • 在%GOROOT%/bin中有三个工具
    • go.exe 编译、运行、构建等都可以使用这个命令
    • godoc.exe 查看包或函数的源码
    • gofmt.exe 格式化文件
1
2
3
4
--bin
	--go.exe
	--godoc.exe
	--gofmt.exe

go.exe参数列表

  • 在命令行中通过go help查看go参数如下
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
Usage:

        go command [arguments]

The commands are:

        build       compile packages and dependencies
        clean       remove object files and cached files
        doc         show documentation for package or symbol
        env         print Go environment information
        bug         start a bug report
        fix         update packages to use new APIs
        fmt         gofmt (reformat) package sources
        generate    generate Go files by processing source
        get         download and install packages and dependencies
        install     compile and install packages and dependencies
        list        list packages
        run         compile and run Go program
        test        test packages
        tool        run specified go tool
        version     print Go version
		vet         report likely mistakes in packages

常用参数解释

  • go version查看Go语言版本
  • go env查看Go语言详细环境
  • go list查看Go语言文件目录
  • go build把源码文件构建成系统可执行文件
  • go clean清空生成的可执行文件
  • go vet静态解析文件,检查是否有语法错误等
  • go get从远程下载第三方Go语言库
  • go bug提交bug
  • go test测试(在后面章节中讲解)
  • go run运行文件

godoc 命令介绍

  • 可以使用godoc [包] [函数名]查看包或函数的详细源码
  • 源码在学习中非常重要,经常查看源码方便理解GO的原理

godoc使用

  • 查看包的源码
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
C:\Users\zhang>godoc fmt
use 'godoc cmd/fmt' for documentation on the fmt command

PACKAGE DOCUMENTATION

package fmt
    import "fmt"

    ...

FUNCTIONS

func Errorf(format string, a ...interface{}) error
    Errorf formats according to a format specifier and returns the string as
    a value that satisfies error.

func Fprint(w io.Writer, a ...interface{}) (n int, err error)
    Fprint formats using the default formats for its operands and writes to
    w. Spaces are added between operands when neither is a string. It
    returns the number of bytes written and any write error encountered.

func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error)
    Fprintf formats according to a format specifier and writes to w. It
    returns the number of bytes written and any write error encountered.

func Fprintln(w io.Writer, a ...interface{}) (n int, err error)
    Fprintln formats using the default formats for its operands and writes
    to w. Spaces are always added between operands and a newline is
    appended. It returns the number of bytes written and any write error
    encountered.

func Fscan(r io.Reader, a ...interface{}) (n int, err error)
    Fscan scans text read from r, storing successive space-separated values
    into successive arguments. Newlines count as space. It returns the
    number of items successfully scanned. If that is less than the number of
    arguments, err will report why.

func Fscanf(r io.Reader, format string, a ...interface{}) (n int, err error)
    Fscanf scans text read from r, storing successive space-separated values
    into successive arguments as determined by the format. It returns the
    number of items successfully parsed. Newlines in the input must match
    newlines in the format.

func Fscanln(r io.Reader, a ...interface{}) (n int, err error)
    Fscanln is similar to Fscan, but stops scanning at a newline and after
    the final item there must be a newline or EOF.

func Print(a ...interface{}) (n int, err error)
    Print formats using the default formats for its operands and writes to
    standard output. Spaces are added between operands when neither is a
    string. It returns the number of bytes written and any write error
    encountered.

func Printf(format string, a ...interface{}) (n int, err error)
    Printf formats according to a format specifier and writes to standard
    output. It returns the number of bytes written and any write error
    encountered.

func Println(a ...interface{}) (n int, err error)
    Println formats using the default formats for its operands and writes to
    standard output. Spaces are always added between operands and a newline
    is appended. It returns the number of bytes written and any write error
    encountered.

func Scan(a ...interface{}) (n int, err error)
    Scan scans text read from standard input, storing successive
    space-separated values into successive arguments. Newlines count as
    space. It returns the number of items successfully scanned. If that is
    less than the number of arguments, err will report why.

func Scanf(format string, a ...interface{}) (n int, err error)
    Scanf scans text read from standard input, storing successive
    space-separated values into successive arguments as determined by the
    format. It returns the number of items successfully scanned. If that is
    less than the number of arguments, err will report why. Newlines in the
    input must match newlines in the format. The one exception: the verb %c
    always scans the next rune in the input, even if it is a space (or tab
    etc.) or newline.

func Scanln(a ...interface{}) (n int, err error)
    Scanln is similar to Scan, but stops scanning at a newline and after the
    final item there must be a newline or EOF.

func Sprint(a ...interface{}) string
    Sprint formats using the default formats for its operands and returns
    the resulting string. Spaces are added between operands when neither is
    a string.

func Sprintf(format string, a ...interface{}) string
    Sprintf formats according to a format specifier and returns the
    resulting string.

func Sprintln(a ...interface{}) string
    Sprintln formats using the default formats for its operands and returns
    the resulting string. Spaces are always added between operands and a
    newline is appended.

func Sscan(str string, a ...interface{}) (n int, err error)
    Sscan scans the argument string, storing successive space-separated
    values into successive arguments. Newlines count as space. It returns
    the number of items successfully scanned. If that is less than the
    number of arguments, err will report why.

func Sscanf(str string, format string, a ...interface{}) (n int, err error)
    Sscanf scans the argument string, storing successive space-separated
    values into successive arguments as determined by the format. It returns
    the number of items successfully parsed. Newlines in the input must
    match newlines in the format.

func Sscanln(str string, a ...interface{}) (n int, err error)
    Sscanln is similar to Sscan, but stops scanning at a newline and after
    the final item there must be a newline or EOF.

TYPES

type Formatter interface {
    Format(f State, c rune)
}
    Formatter is the interface implemented by values with a custom
    formatter. The implementation of Format may call Sprint(f) or Fprint(f)
    etc. to generate its output.

type GoStringer interface {
    GoString() string
}
    GoStringer is implemented by any value that has a GoString method, which
    defines the Go syntax for that value. The GoString method is used to
    print values passed as an operand to a %#v format.

...
  • 查看某个包中某个函数
1
2
3
4
5
6
7
8
C:\Users\zhang>godoc fmt Println
use 'godoc cmd/fmt' for documentation on the fmt command

func Println(a ...interface{}) (n int, err error)
    Println formats using the default formats for its operands and writes to
    standard output. Spaces are always added between operands and a newline
    is appended. It returns the number of bytes written and any write error
    encountered.

gofmt工具介绍

  • 规范的代码方便自己的阅读也方便别人的阅读.编写规范代码是每个程序的必修课
  • gofmt工具可以帮助程序员把代码进行格式化,按照规范进行格式化
  • 使用gofmt前提是文件编译通过

不规范代码示例

  • 查看下面代码中不规范的地方有几处
1
2
3
4
5
package main
import "fmt"
func main ( ){
fmt.Println("hello word");
}

使用gofmt的步骤

  • 在命令行输入gofmt 文件名就可以对文件进行格式化,格式化后输出
1
2
3
4
5
6
7
8
D:\go\0201>gofmt main.go
package main

import "fmt"

func main() {
        fmt.Println("hello word")
}
  • 通过运行gofmt后发现规范的代码和不规范代码的几处区别
    • package关键字和import关键字和func main之间有空行
    • main和括号之间没有空格
    • main后面()之间没有空格
    • ()和{之间有空格
    • fmt.Println()前面有缩进
    • fmt.Println()后面没有分号

golang学习一:从环境配置开始到HelloWorld入门 golang学习二:golang自带的工具 olang学习三:golang基础语法 golang学习四:流程控制 golang学习五:常用数学函数与数组 golang学习六:for循环 golang学习七:goto和label golang学习八:切片 golang学习九:sort包、map、双向链表、双向循环链表 golang学习十:函数 golang学习十一:包的访问权限、变量作用域、闭包 golang学习十二:值传递和引用传递 golang学习十三:结构体 golang学习十四:golang中的面向对象 golang学习十五:错误异常处理 golang学习十六:文件操作 golang学习十七:反射 golang学习十八:XML操作 golang学习十九:日志 golang学习二十:golang并发编程入门 golang学习二十一:select和GC

Licensed under CC BY-NC-SA 4.0