Statement
A statement is a single logical instruction in the Go programming language that completes a specific operation. Statements are the basic units that make up programs, they can control program flow, assign values, declare variables, etc.
1. Declaration statement
1.1 Variable declaration
Used to define one or more variables, but do not necessarily have to be assigned values.
Give an example
var age int var name, address string
1.2 Constant declaration
Defines one or more constant values.
Give an example
const PI = 3.14 const greeting = "Hello, Go!"
2. Assignment statement
Used to assign new values to declared variables.
Give an example
x := 10 y = x + 5 a, b := 20, 30
3. Control flow statement
3.1 Conditional Statement
if statement
Execute code blocks based on a certain condition.
Give an example
if x > y { ("x is greater than y") } else if x < y { ("x is less than y") } else { ("x is equal to y") }
Switch statement
Execute one of multiple code blocks based on an expression or value.
Give an example
switch grade { case "A": ("Excellent!") case "B": ("Good") default: ("Passed") }
3.2 Loop statements
for statement
Used to repeatedly execute a piece of code.
Give an example
for i := 0; i < 10; i++ { (i) } for _, value := range array { (value) }
3.3 Jump statement
break statement
Used to interrupt the current loop.
Give an example
for i := 0; i < 10; i++ { if i == 5 { break } (i) }
Continue statement
Skip the current iteration and continue with the next iteration.
Give an example
for i := 0; i < 10; i++ { if i%2 == 0 { continue } (i) }
Return statement
Returns a specific value from the function.
Give an example
func add(a int, b int) int { return a + b }
goto statement
Jump to the specified tag.
Give an example
for i := 0; i < 10; i++ { if i == 5 { goto end } (i) end: }
4. Other statements
4.1 defer statement
Make sure to execute a statement before the function ends.
Give an example
func printFile() { file, err := ("") if err != nil { panic(err) } defer () // Do file operations... }
4.2 go statement
Execute function calls in a new goroutine.
Give an example
go func() { ("Executing in a new goroutine") }()
Practical cases
Statement | Statement example |
---|---|
Variable declaration | var age int、var name, address string、var x, y int = 3, 4、var active bool、var salary = 50000 |
Constant declaration | const PI = 3.14、const greeting = "Hello, Go!"、const active = false、const daysInWeek = 7、const lightSpeed = 299792458 |
Assignment statement | x := 10、y = x + 5、a, b := 20, 30、name = "Alice"、isActive := true |
if statement | if x > 10 { ... }、if x > 10 && y < 5 { ... }、if active { ... }、if name := getName(); name != "" { ... }、if age > 18 { ... } else { ... } |
Switch statement | switch x { ... }、switch { case x > 10: ... }、switch day { case "Monday": ... }、switch n := 4; n { ... }、switch y.(type) { ... } |
for statement | for i := 0; i < 5; i++ { ... }、for i, v := range arr { ... }、for x > 5 { ... }、for key, val := range mapData { ... }、for _, char := range str { ... } |
break statement | for { if condition { break } }、switch { case x: if y { break } }、for x > 10 { ...; break; ... }、label: for { ...; break label; ... }、for i := 0; i < 10; i++ { if i == 5 { break } } |
Continue statement | for i := 0; i < 10; i++ { if i%2 == 0 { continue } }、for _, v := range data { if v == nil { continue } }、for x > 0 { ...; if condition { continue } ... }、for { if !isValid(data) { continue } ... }、for idx, value := range items { if value == "" { continue } } |
Return statement | func add(a, b int) int { return a + b }、func name() string { return "Alice" }、func getDetails() (string, int) { return "Alice", 30 }、func isActive() bool { ...; return false }、func calculate() float64 { ...; return result } |
goto statement | label1: for { ...; if x > 5 { goto label1 } }、label2: ("Start"); ...; goto label2、if condition { goto errorHandling } ... errorHandling: ...、if !isValid { goto cleanup } ... cleanup: ... |
defer statement | file, _ := (""); defer ()、(); defer ()、defer ("Finished!")、(); defer ()、reader := openReader(); defer () |
Go statement | go ("Running in goroutine")、go process(data)、go func(val int) { ... }(x)、go startServer()、go handleRequest(request) |
Introduction, detailed explanation, examples of expressions
In programming, an expression is a structure that combines variables, constants, and operators in some way and can be evaluated as a certain value. In Go, expressions can take many forms depending on the content and results it contains.
1. Basic expressions
1.1 Literal
A literal is an expression that represents a fixed value.
Give an example
42 // Integer literal3.14 // Floating point literaltrue // Boolean literal"Hello" // String literal
1.2 Variables and constants
Variables and constants are predefined entities with specific names and values.
Give an example
const PI = 3.14 var name = "Go"
2. Compound expressions
2.1 Arithmetic expressions
These expressions use arithmetic operators such as +, -, *, / and %.
Give an example
a := 5 b := 2 sum := a + b // Results: 7difference := a - b // Results: 3product := a * b // Results: 10quotient := a / b // Results: 2remainder := a % b // result:1
2.2 Relational expressions
Relational expressions are evaluated as Boolean values, and commonly used relational operators are ==, !=, <, <=, > and >=.
Give an example
x := 5 y := 3 result1 := x == y // Result: falseresult2 := x > y // result:true
2.3 Logical expressions
Logical expressions are used to combine multiple Boolean expressions. Commonly used logical operators are &&, || and!.
Give an example
a := true b := false result1 := a && b // Result: falseresult2 := a || b // Result: trueresult3 := !a // result:false
2.4 Assignment expressions
The assignment expression assigns a value to the variable and returns the value.
Give an example
x := 10 // Use := for assignmenty = x + 5 // use = Perform assignment
3. Function call expression
The function call returns the return value of the function.
Give an example
func add(a int, b int) int { return a + b } result := add(5, 3) // result:8
4. Type conversion expression
These expressions convert values from one type to another.
Give an example
x := 5.8 y := int(x) // result:5
Practical cases
Statement | Statement example |
---|---|
Variable declaration | var age int、var name, address string、var x, y int = 3, 4、var active bool、var salary = 50000 |
Constant declaration | const PI = 3.14、const greeting = "Hello, Go!"、const active = false、const daysInWeek = 7、const lightSpeed = 299792458 |
Assignment statement | x := 10、y = x + 5、a, b := 20, 30、name = "Alice"、isActive := true |
if statement | if x > 10 { ... }、if x > 10 && y < 5 { ... }、if active { ... }、if name := getName(); name != "" { ... }、if age > 18 { ... } else { ... } |
Switch statement | switch x { ... }、switch { case x > 10: ... }、switch day { case "Monday": ... }、switch n := 4; n { ... }、switch y.(type) { ... } |
for statement | for i := 0; i < 5; i++ { ... }、for i, v := range arr { ... }、for x > 5 { ... }、for key, val := range mapData { ... }、for _, char := range str { ... } |
break statement | for { if condition { break } }、switch { case x: if y { break } }、for x > 10 { ...; break; ... }、label: for { ...; break label; ... }、for i := 0; i < 10; i++ { if i == 5 { break } } |
Continue statement | for i := 0; i < 10; i++ { if i%2 == 0 { continue } }、for _, v := range data { if v == nil { continue } }、for x > 0 { ...; if condition { continue } ... }、for { if !isValid(data) { continue } ... }、for idx, value := range items { if value == "" { continue } } |
Return statement | func add(a, b int) int { return a + b }、func name() string { return "Alice" }、func getDetails() (string, int) { return "Alice", 30 }、func isActive() bool { ...; return false }、func calculate() float64 { ...; return result } |
goto statement | label1: for { ...; if x > 5 { goto label1 } }、label2: ("Start"); ...; goto label2、if condition { goto errorHandling } ... errorHandling: ...、if !isValid { goto cleanup } ... cleanup: ... |
defer statement | file, _ := (""); defer ()、(); defer ()、defer ("Finished!")、(); defer ()、reader := openReader(); defer () |
Go statement | go ("Running in goroutine")、go process(data)、go func(val int) { ... }(x)、go startServer()、go handleRequest(request) |
The above is the detailed content of the in-depth analysis of the Go statement and expression case manual. For more information about Go statement expression cases, please pay attention to my other related articles!