小白教程
所有教程
关于
Search
172.71.223.89
172.71.223.89
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“Go 语言基础语法”的源代码
本页内容
上一节:
Go_语言结构
下一节:
Go_语言数据类型
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:Go 语言基础语法}}[[Category:Goalng 教程|4]] = Go 语言基础语法 = 上一章节我们已经了解了 Go 语言的基本组成结构,本章节我们将学习 Go 语言的基础语法。 == Go 标记 == Go 程序可以由多个标记组成,可以是关键字,标识符,常量,字符串,符号。如以下 GO 语句由 6 个标记组成: <pre> fmt.Println("Hello, World!") </pre> 6 个标记是(每行一个): <pre> 1. fmt 2. . 3. Println 4. ( 5. "Hello, World!" 6. ) </pre> == 行分隔符 == 在 Go 程序中,一行代表一个语句结束。每个语句不需要像 C 家族中的其它语言一样以分号 ; 结尾,因为这些工作都将由 Go 编译器自动完成。 如果你打算将多个语句写在同一行,它们则必须使用 ; 人为区分,但在实际开发中我们并不鼓励这种做法。 以下为两个语句: <sample title="" desc="" lang="go" hererun="1"> fmt.Println("Hello, World!") fmt.Println("小白教程:xiaobai.wang") </sample> == 注释 == 注释不会被编译,每一个包应该有相关注释。 单行注释是最常见的注释形式,你可以在任何地方使用以 // 开头的单行注释。多行注释也叫块注释,均已以 /* 开头,并以 */ 结尾。如: <pre> // 单行注释 /* Author by 小白教程 我是多行注释 */ </pre> == 标识符 == 标识符用来命名变量、类型等程序实体。一个标识符实际上就是一个或是多个字母(A~Z和a~z)数字(0~9)、下划线_组成的序列,但是第一个字符必须是字母或下划线而不能是数字。 以下是有效的标识符: <pre> mahesh kumar abc move_name a_123 myname50 _temp j a23b9 retVal </pre> 以下是无效的标识符: * 1ab(以数字开头) * case(Go 语言的关键字) * a+b(运算符是不允许的) == 字符串连接 == Go 语言的字符串连接可以通过 + 实现: <sample title="" desc="" lang="go" hererun="1"> package main import "fmt" func main() { fmt.Println("Google" + "xiaoBai") } </sample> 以上实例输出结果为: <pre>GooglexiaoBai</pre> == 关键字 == 下面列举了 Go 代码中会使用到的 25 个关键字或保留字: {| class="table table-striped table-hover" | break | default | func | interface | select |- | case | defer | go | map | struct |- | chan | else | goto | package | switch |- | const | fallthrough | if | range | type |- | continue | for | import | return | var |} 除了以上介绍的这些关键字,Go 语言还有 36 个预定义标识符: {| class="table table-striped table-hover" | append | bool | byte | cap | close | complex | complex64 | complex128 | uint16 |- | copy | false | float32 | float64 | imag | int | int8 | int16 | uint32 |- | int32 | int64 | iota | len | make | new | nil | panic | uint64 |- | print | println | real | recover | string | true | uint | uint8 | uintptr |} 程序一般由关键字、常量、变量、运算符、类型和函数组成。 程序中可能会使用到这些分隔符:括号 (),中括号 [] 和大括号 {}。 程序中可能会使用到这些标点符号:.、,、;、: 和 …。 == Go 语言的空格 == Go 语言中变量的声明必须使用空格隔开,如: <pre>var age int;</pre> 语句中适当使用空格能让程序更易阅读。 无空格: <pre>fruit=apples+oranges;</pre> 在变量与运算符间加入空格,程序看起来更加美观,如: <pre> fruit = apples + oranges; </pre> == 格式化字符串 == Go 语言中使用 fmt.Sprintf 格式化字符串并赋值给新串: <sample title="" desc="" lang="go" hererun="1"> package main import ( "fmt" ) func main() { // %d 表示整型数字,%s 表示字符串 var stockcode=123 var enddate="2020-12-31" var url="Code=%d&endDate=%s" var target_url=fmt.Sprintf(url,stockcode,enddate) fmt.Println(target_url) } </sample> 输出结果为: <pre>Code=123&endDate=2020-12-31</pre> 更多内容参见:[https://www.xiaobai.wang/go/go-fmt-sprintf.html Go fmt.Sprintf 格式化字符串]
返回至“
Go 语言基础语法
”。
上一节:
Go_语言结构
下一节:
Go_语言数据类型