Go Language Keyword Reference Guide
Go language is a concise and fast compiled programming language with excellent concurrency capabilities. One of the first steps in learning the Go language is to become familiar with its keywords.
Keywords are reserved words with special meaning and cannot be used as variable, function or type names. The following table summarizes all Go keywords:
Keyword | Meaning |
---|---|
break |
Terminate a loop or switch statement |
case |
Define conditions in a switch statement |
chan |
Create or receive channel |
const |
Declare constants |
continue |
Skip the current iteration of the loop |
default |
Define the default situation in the switch statement |
defer |
Execute a before the function returns Or multiple functions |
else |
The veto condition of the expression block |
fallthrough |
Allow multiple situations to be handled in a switch statement |
for |
Create a loop |
func |
Define function |
go |
Start coroutine |
if |
Expression condition block |
import | Import external package |
interface |
Define interface type |
map |
Create mapping type |
package |
Define package |
range |
Traverse a collection in an iterator |
return |
Return an or from a function Multiple values |
select |
for concurrent receive communication |
struct |
Define structure type |
switch |
Execute different code blocks based on conditions |
type |
Define type |
var |
Define variable |
Practical case
The following is an example using some common Go keywords:
package main import "fmt" func main() { // 定义常量 const myConst = 10 for i := 0; i < myConst; i++ { fmt.Println(i) } // 使用 fallthrough 在 switch 语句中处理多个情况 switch i { case 1: fmt.Println("i is 1") fallthrough case 2: fmt.Println("i is 2") } }
Output:
0 1 2 i is 1 i is 2
Hope this guide can help you understand and use Go keywords to write Go programs more efficiently.
The above is the detailed content of Go language keyword reference guide. For more information, please follow other related articles on the PHP Chinese website!