从零开始——GO语言基础语法
时间:2023-09-09 08:16:44
学习任何一门语言,都要学习好基础,把基础打牢,那些框架对你来说都是工具,你自己的基础好,懂得了他们的原理,自己就可以创造更优秀的框架。现在带大家学习GO语言的基础语法,让您的编程路上不迷茫。
Go 语言基础语法
Go 程序可以由多个标记组成,可以是关键字,标识符,常量,字符串,符号。如以下 GO 语句由 6 个标记组成:
fmt.Println("Hello, World!")
6 个标记是(每行一个):
1. fmt2. . 3. Println 4. ( 5. "Hello, World!" 6. )行分隔符
在 Go 程序中,一行代表一个语句结束。每个语句不需要像 C 家族中的其它语言一样以分号 ; 结尾,因为这些工作都将由 Go 编译器自动完成。
如果你打算将多个语句写在同一行,它们则必须使用 ; 人为区分,但在实际开发中我们并不鼓励这种做法。
以下为两个语句:
fmt.Println("Hello, World!") fmt.Println("macz")注释
注释不会被编译,每一个包应该有相关注释。
单行注释是最常见的注释形式,你可以在任何地方使用以 // 开头的单行注释。多行注释也叫块注释,均已以 /* 开头,并以 */ 结尾。如:
// 单行注释 /* 我是多行注释 */标识符
标识符用来命名变量、类型等程序实体。一个标识符实际上就是一个或是多个字母(A~Z和a~z)数字(0~9)、下划线_组成的序列,但是第一个字符必须是字母huo下划线而不能是数字。
以下是有效的标识符:
mahesh kumar abc move_name a_123 myname50 _temp j a23b9 retVal
以下是无效的标识符:
1ab(以数字开头)
case(Go 语言的关键字)
a+b(运算符是不允许的)
字符串连接
Go 语言的字符串可以通过 + 实现:
实例
package mAInimport "fmt"func mAIn(){ fmt.Println("Google"+"BAIdu") }
以上实例输出结果为:
GoogleBAIdu
关键字
下面列举了 Go 代码中会使用到的 25 个关键字或保留字:
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 个预定义标识符:
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 |
println | real | recover | string | true | uint | uint8 | uintptr |
程序一般由关键字、常量、变量、运算符、类型和函数组成。
程序中可能会使用到这些分隔符:括号 (),中括号 [] 和大括号 {}。
程序中可能会使用到这些标点符号:.、,、;、: 和 …。
Go 语言的空格
Go 语言中变量的声明必须使用空格隔开,如:
var age int;
语句中适当使用空格能让程序看易阅读。
无空格:
fruit=apples+oranges;
在变量与运算符间加入空格,程序看起来更加美观,如:
fruit = apples + oranges;
希望小编的分享能够帮助到您,了解更多Mac编程软件教程,尽在我们!
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Excel如何制作圆环图?Excel制作圆环图方法
![](https://img.aspxhome.com/file/2023/9/31419_0s.jpg)
Win8如何禁止应用商店显示通知
![](https://img.aspxhome.com/file/2023/29/a251831_0s.jpg)
WPS Office套件上线Win10应用商店:免费+内购
![](https://img.aspxhome.com/file/2023/5/a313551_0s.jpg)
华为电脑怎么更新win11?华为电脑安装win11系统教程
![](https://img.aspxhome.com/file/2023/30/a270797_0s.png)
如何在PowerPoint中添加背景音乐
![](https://img.aspxhome.com/file/2023/10/a345256_0s.jpg)
Win11网速慢被限速怎么办?Win11系统网速限制解除技巧
![](https://img.aspxhome.com/file/2023/1/a275683_0s.jpg)
Excel2013必备的4个生产力技巧
win10电脑玩上古卷轴5画面卡死的解决方法
![](https://img.aspxhome.com/file/2023/4/a310639_0s.jpg)
刷机精灵怎么用?怎么用刷机精灵刷机?
![](https://img.aspxhome.com/file/2023/9/a340655_0s.jpg)
Excel中如何批量录入序列和日期
![](https://img.aspxhome.com/file/2023/9/a140099_0s.jpg)
word2003只能用安全模式打开怎么办
![](https://img.aspxhome.com/file/2023/7/21257_0s.jpg)
win10更新并关机怎么解决
![](https://img.aspxhome.com/file/2023/7/50927_0s.jpg)
Excel中表格下划线不显示的解决方法
企业如何使用Mac钉钉建群?
![](https://img.aspxhome.com/file/2023/4/a214154_0s.png)
Win10开机后进入系统黑屏怎么办?
![](https://img.aspxhome.com/file/2023/7/51467_0s.png)
WPS表格如何打印报表表头?
![](https://img.aspxhome.com/file/2023/9/a171059_0s.png)
手机摄像头驱动,小编告诉你怎样使用手机摄像头驱动
![](https://img.aspxhome.com/file/2023/1/a276789_0s.png)
Win11打开应用时会弹出是否允许更改怎么解决?
![](https://img.aspxhome.com/file/2023/1/a275248_0s.jpg)
XP系统如何设置锁定状态下电脑仍正常运作?
![](https://img.aspxhome.com/file/2023/3/a301209_0s.jpg)
Win7旗舰版开机提示dsark64.sys文件损坏要解决方法
![](https://img.aspxhome.com/file/2023/1/a280933_0s.jpg)