【Golang关键字用法详细指南】关键字或保留字是用于某些内部过程或表示某些预定义动作的语言中的字。因此, 不允许将这些单词用作标识符。这样做会导致编译时错误。
例子:
// 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语言中如下所示:
例子:
// 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: lsbinLanguage name: Go LanguageChapter name: Keywords
推荐阅读