2.1 gofmt 的文化演变## gofmt 的文化演变 The Cultural Evolution of gofmt Robert Griesemer Google, Inc. ## gofmt • Go源代码格式化工具 • 定义了“标准”格式 • golang.org代码库中所有提交的Go代码都必须通过gofmt格式化过 - 除了gofmt之外,相同功能可以通过go/format库获得 - 不需要设置! ## ## 尽量保证其简单 • 小的语言能让事情变得简单 不要为行长度烦恼 相反的,尊重用户:考虑原有代码中的断行 - 不要支持任何选项 • 使其使用傻瓜化 一个格化标准搞定所有! ## gofmt的基本结构 • 源代码的处理 - 基本的格式化 • 附加:注释的处理 - 完善:代码和注释的对齐 但是,没有牛X的通用布局算法 • 相反的:基于节点的精细优化 ## 处理源代码 - 使用`go/scanner` a7129c0131c34f1da/p23_1.jpg) ## gofmt 的应用 ## gofmt 作为源代码变换工具 • 改写 Go 的代码 (Russ Cox), gofmt -r gofmt -w -r 'a[i:len(x)] -> a[i:]' *.go - 简化 Go 的代码,gofmt -s - 更新 API (Russ Cox), go fix •0 码力 | 34 页 | 9.97 MB | 2 年前3
Golang Manual By AstaXie-20120522With Go we take an unusual approach and let the machine take care of most formatting issues. The gofmt program (also available as go fmt, which operates at the package level rather than source file level) to know how to handle some new layout situation, run gofmt; if the answer doesn't seem right, rearrange your program (or file a bug about gofmt), don't work around it. As an example, there's fields of a structure. Gofmt will do that for you. Given the declaration ```cpp type T struct { name string // name of the object value int // its value } ``` gofmt will line up the columns:0 码力 | 6205 页 | 12.83 MB | 2 年前3
The Way To Go - 2012Building and running go-programs with command- and Makefiles.....35 3.5 Formatting code: go fmt or gofmt.....39 3.6 Documenting code: go doc or godoc.....40 3.7 Other tools.....41 3.8 Go's performance compiler: When a program has been written (applying common naming- and style-rules), subject it to gofmt (see §3.5 to format it correctly), and then build (compile / link) it; if the build-process (which code formatting: If you append the following lines to Makefile: format: gofmt -w $(GOFILES) then gomake format will invoke gofmt on your source-files. ## I vo Balbaert ## Makefile in Windows: A Makefile0 码力 | 629 页 | 4.85 MB | 2 年前3
Go 入门指南(The way to Go)时所有被调用函数的列表)。 4. 使用关键字 defer 来跟踪代码执行过程(第 6.4 节)。 ### 3.4 构建并运行 Go 程序 在大多数 IDE 中,每次构建程序之前都会自动调用源码格式化工具 gofmt 并保存格式化后的源文件。如果构建成功则不会输出任何信息,而当发生编译时错误时,则会指明源码中具体第几行出现了什么错误,如:a declared and not used。一般情况下,你可以双击 IDE 浪费大量宝贵的开发时间,因此他们制作了一个工具:go fmt(gofmt)。这个工具可以将你的源代码格式化成符合官方统一标准的风格,属于语法风格层面上的小型重构。遵循统一的代码风格是 Go 开发中无可撼动的铁律,因此你必须在编译或提交版本管理系统之前使用 gofmt 来格式化你的代码。 尽管这种做法也存在一些争论,但使用 gofmt 后你不再需要自成一套代码风格而是和所有人使用相同的规则。这不仅增强了代码的可读性,而且在接手外部 tab 而不替换成空格。 在命令行输入 gofmt -w program.go 会格式化该源文件的代码然后将格式化后的代码覆盖原始内容(如果不加参数 -w 则只会打印格式化后的结果而不重写文件);gofmt -w *.go 会格式化并重写所有 Go 源文件;gofmt map1 会格式化并重写 map1 目录及其子目录下的所有 Go 源文件。 gofmt 也可以通过在参数 -r 后面加入用双引号0 码力 | 380 页 | 2.97 MB | 2 年前3
Go 入门指南(The way to Go)defer 来跟踪代码执行过程(第 6.4 节)。 ### 3.4 构建并运行 Go 程序 ### 3.4 构建并运行 Go 程序 在大多数 IDE 中,每次构建程序之前都会自动调用源码格式化工具 gofmt 并保存格式化后的源文件。如果构建成功则不会输出任何信息,而当发生编译时错误时,则会指明源码中具体第几行出现了什么错误,如:a declared and not used。一般情况下,你可以双击 IDE 浪费大量宝贵的开发时间,因此他们制作了一个工具:go fmt(gofmt)。这个工具可以将你的源代码格式化成符合官方统一标准的风格,属于语法风格层面上的小型重构。遵循统一的代码风格是 Go 开发中无可撼动的铁律,因此你必须在编译或提交版本管理系统之前使用 gofmt 来格式化你的代码。 尽管这种做法也存在一些争论,但使用 gofmt 后你不再需要自成一套代码风格而是和所有人使用相同的规则。这不仅增强了代码的可读性,而且在接手外部 tab 而不替换成空格。 在命令行输入 gofmt -w program.go 会格式化该源文件的代码然后将格式化后的代码覆盖原始内容(如果不加参数 -w 则只会打印格式化后的结果而不重写文件);gofmt -w *.go 会格式化并重写所有 Go 源文件;gofmt map1 会格式化并重写 map1 目录及其子目录下的所有 Go 源文件。 gofmt 也可以通过在参数 `-r` 后面加入用双0 码力 | 466 页 | 4.44 MB | 2 年前3
deno handbookof code, e.g. deno eval "console.log(1 + 2)" - fmt a built-in code formatter (similar to gofmt in Go) • help prints this message or the help of the given subcommand(s) • info show info about code One of the things I really liked from Go was the gofmt command that came with the Go compiler. All Go code looks the same. Everyone uses gofmt. JavaScript programmers0 码力 | 46 页 | 4.11 MB | 2 年前3
Build web application with Golangproject to Github. These are useful for local tests, but useless for version control. ## go fmt and gofmt The people who are working with C/C++ should know that people are always arguing about which code command 'gofmt -l -w' on the packages named by the import paths. We usually use gofmt -w instead of go fmt. The latter will not rewrite your source files after formatting code. gofmt -w src formats expression using F1 - Function declaration jump using F2 - Gdb support - Auto-format with gofmt - Others - Multi-language - Plugin system - Text editor themes - Syntax support based0 码力 | 327 页 | 1.63 MB | 2 年前3
Go Web编程sources env print Go environment information fix run go tool fix on packages fmt run gofmt on package sources get download and install packages and dependencies install compile and 工具集中提供了一个gofmt命令它可以帮你格式化你写好的代码文件,使你写代码的时候不需要关心格式,你只需要在写完之后执行gofmt<文件名>.go,你的代码就被修改成了标准格式,但是我平常很少用到这个命令,因为开发工具里面一般都带了保存时候自动格式化功能,这个功能其实在底层就是调用了gofmt。接下来的一节我将讲述两个工具,这两个工具都自带了保存文件时自动化gofmt功能。 使用go 使用go fmt命令,更多时候是用gofmt,而且需要参数-w,否则格式化结果不会写入文件。gofmt -w src,可以格式化整个项目。 ## go get 这个命令是用来动态获取远程代码包的,目前支持的有BitBucket、GitHub、Google Code和Launchpad。这个命令在内部实际上分成了两步操作:第一步是下载源码包,第二步是执行go install。下载源码包的go工具会自动根0 码力 | 295 页 | 5.91 MB | 2 年前3
The Go Programming Language (Otc 30, 2009)gotest) networking, HTTP, RPC HTML (and more general) templates ...and lots more. ## Godoc and Gofmt ## Godoc: documentation server, analogous to javadoc but easier on the programmer. Can run yourself serves all docs) http://golang.org/pkg/ (package docs) http://golang.org/src/ (source code) ## Gofmt: pretty-printer; all code in the repository has been formatted by it. ## Debugger A custom debugger0 码力 | 47 页 | 241.70 KB | 2 年前3
Real World Goformatter: gofmt. We never argued over where to put a curly-brace, tabs vs. spaces, or if we should align assignments. We simply agreed that the buck stopped at the default output from gofmt." ## MROffice0 码力 | 49 页 | 595.19 KB | 2 年前3
共 38 条
- 1
- 2
- 3
- 4













