Home > Backend Development > Golang > How to use if in go language

How to use if in go language

青灯夜游
Release: 2023-01-30 18:49:12
Original
5268 people have browsed it

In the Go language, the keyword if is used to test whether a certain condition (Boolean or logical) is true. If the condition is true, the if followed by curly brackets "{}" will be executed. code block, otherwise the code block will be ignored and the subsequent code will be executed. The syntax is "if condition {...}". The if statement is used to select a single branch. If there is a second branch, it needs to be used with the else keyword; if there are more branches, it needs to be used with the "else if" keyword.

How to use if in go language

The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.

Go language if else (branch structure)

In the Go language, the keyword if is used for testing A statement with a certain condition (Boolean or logical type). If the condition is true, the code block enclosed by braces {} after the if will be executed. Otherwise, the code block will be ignored and the subsequent execution will continue. code.

if condition {
    // do something
}
Copy after login

If there is a second branch, you can add the else keyword and another code block based on the above code. The code in this code block will only be executed when the condition is not met, if and The two code blocks after else are independent branches, and only one of them can be executed.

if condition {
    // do something
} else {
    // do something
}
Copy after login

The if else statement is an upgraded version of the if statement. It can not only specify the code to be executed when the expression is true, but also the code to be executed when the expression is not true

If there is a third branch, you can use the following form of three independent branches:

if condition1 {
    // do something
} else if condition2 {
    // do something else
}else {
    // catch-all or default
}
Copy after login

else if keyword is used when the conditions of the if statement are not met. , continue to execute the logical judgment in else if, and if the conditions are met, execute the logic in else if braces.

else if statement can only be written after the braces of the if statement, and any number of else if statements can be written.

There is no limit to the number of else if branches, but for the sake of code readability, it is better not to add too many else if structures after if. If you must use this form, try to satisfy the requirements first. conditions in front.

The opening brace after the keywords if and else{ must be on the same line as the keyword. If you use the else if structure, the closing brace of the previous code block }Must be on the same line as the else if keyword. These two rules are enforced by the compiler.

Illegal Go code:

if x{
}
else { // 无效的
}
Copy after login

It should be noted that after using gofmt to format the code, the code in each branch will be indented by 4 or 8 spaces, or 1 tab, and the closing brace } is vertically aligned with the corresponding if keyword.

In some cases, the parentheses on both sides of the conditional statement can be omitted. When the conditions are complex, you can use parentheses to make the code more readable. You can use parentheses when using &&, || or! To increase the operation priority of an expression and improve the readability of the code.

Example

Use the following example to understand how to write if:

var ten int = 11
if ten > 10 {
    fmt.Println(">10")
} else {
    fmt.Println("<=10")
}
Copy after login

The code output is as follows:

>10
Copy after login

The code description is as follows :

  • Line 1, declare an integer variable and assign a value of 11.

  • Line 2, determine when the value of ten is greater than 10, execute line 3, otherwise execute line 4.

  • Lines 3 and 5 print the output when it is greater than 10 and less than or equal to 10 respectively.

Special writing method

if There is also a special writing method, you can add an execution statement before the if expression , and then judge based on the variable value, the code is as follows:

if err := Connect(); err != nil {
    fmt.Println(err)
    return
}
Copy after login

Connect is a function with a return value, err:=Connect() is a statement, after executing Connect, the error is saved to the err variable.

err != nil is the judgment expression of if. When err is not empty, an error is printed and returned.

This way of writing can put the return value and judgment in one line for processing, and the scope of the return value is limited to the if and else statement combination.

Tips

In programming, the smaller the scope of a variable, the smaller the possibility of problems caused. Each variable represents a state, and where there is a state , the status will be modified. The local variables of the function will only affect the execution of one function, but the global variables may affect the execution status of all codes. Therefore, limiting the scope of variables is of great help to the stability of the code.

【Related recommendations: Go video tutorial, Programming teaching

The above is the detailed content of How to use if in go language. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template