一 注释
1 点睛
用于注解说明解释程序的文字就是注释,注释提高了代码的阅读性。
注释是一个程序员必须药具有的良好的编程习惯。将自己的思想通过注释先整理出来,再用代码去体现。
2 注释的两种形式
a 行注释
// 注释内容
// 示例
package main
import "fmt" // fmt包中提供格式化,输出,输入的函数.
func main() {
fmt.Println("姓名\t年龄\t籍贯\t地址\njohn\t20\t河北\t北京")
}
b 块注释
/*
注释内容
*/
// 示例
func main() {
/*fmt.Println("姓名\t年龄\t籍贯\t地址\njohn\t20\t河北\t北京")*/
}
3 使用细节
-
对于行注释和块注释,被注释的文字,不会被 Go 编译器执行。
-
块注释里不允许有块注释嵌套。
二 规范的代码风格
1 正确的注释和注释风格
Go 官方推荐使用行注释来注释整个方法和语句。
Go 源码都是采用行注释来注释的。
2 正确的缩进和空白
a 使用一次 tab 操作,实现缩进,默认整个向右移动,使用 shift + tab,整体左移。
b 使用 gofmt 来进行格式化。
c 运算符两边习惯性各加一个空格。
var num = 2 + 4 * 5
d Go 语言的代码风格
下面是正确写法:
package main
// fmt包中提供格式化,输出,输入的函数.
import "fmt"
func main() {
fmt.Println("姓名\t年龄\t籍贯\t地址\njohn\t20\t河北\t北京")
}
下面是错误写法:
package main
// fmt包中提供格式化,输出,输入的函数.
import "fmt"
func main()
{
fmt.Println("姓名\t年龄\t籍贯\t地址\njohn\t20\t河北\t北京")
}
Go 设计思想:一个问题尽量只有一个解决方法。
e 一行最长不超过 80 个字符,超过的请使用换行展示,尽量保持格式优雅
package main
import "fmt" // fmt包中提供格式化,输出,输入的函数.
// 这是一个main函数,是程序入口
func main() {
fmt.Println("helloworldhelloworldhelloworldhelloworl\n",
"dhelloworldhelloworldhelloworldhelloworldhelloworldhellowor\n",
"ldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworl\n",
"dhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhel\n",
"loworldhelloworldhelloworldhelloworldhelloworldhelloworldhellowor\n",
"ldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworld")
}
三 Golang 官方编程指南
1 官网
2 Golang 官方标准库 API 文档,可以查看 Golang 所有包下的函数和使用。
3 解释术语
api:application program interface:应用程序编程接口。
简言之:就是 Go 的各个包的各个函数。
四 Golang 标准库 API 文档
1 API 是 Golang 提供的基本编程接口。
2 Go 语言提供了大量的标准库,因此 google 公司也为这些标准库提供了响应的 API 文档,用于告诉开发者如何使用这些标准库,以及标准库包含的方法。
3 Golang 中文网在线标准库文档:https://studygolang.com/pkgdoc
4 Golang 的包、源文件和函数的关系