关键字或保留字是用于某些内部过程或表示某些预定义动作的语言中的字。因此, 不允许将这些单词用作标识符。这样做会导致编译时错误。
例子:
// Go program to illustrate the
// use of keywords
package main
import "fmt"
// Here, package, import, func, // var are keywords
func main() {
// Here, a is a valid identifier
var a = "lsbin"
fmt.Println(a)
// Here, the default is an
// illegal identifier and
// compiler will throw an error
// var default = "GFG"
}
输出如下:
lsbin
有共有25个关键字在Go语言中如下所示:
打破 |
案件 |
chan |
const |
继续 |
default |
推迟 |
其他 |
失败 |
对于 |
功能 |
go |
去 |
if |
import |
接口 |
地图 |
包 |
范围 |
返回 |
查找 |
结构 |
开关 |
类型 |
变种 |
例子:
// Go program to illustrate
// the use of keywords
// Here package keyword is used to
// include main package in the program
package main
// import keyword is used to
// import "fmt" in your package
import "fmt"
// func is used to
// create function
func main() {
// Here, var keyword is used
// to create variables
// Pname, Lname, and Cname
// are the valid identifiers
var Pname = "lsbin"
var Lname = "Go Language"
var Cname = "Keywords"
fmt.Printf( "Portal name: %s" , Pname)
fmt.Printf( "\nLanguage name: %s" , Lname)
fmt.Printf( "\nChapter name: %s" , Cname)
}
输出如下:
Portal name: lsbin
Language name: Go Language
Chapter name: Keywords