banner
音小董

哩哔哩哔

这世上的热闹出自孤单

Chapter 3: Identifiers, Operators, and Flow Control

Identifier#

What is an Identifier#

==Answer: The name given to variables, methods, etc. is called an identifier.==

Rules for defining identifiers#

1. Composed of 26 English letters (uppercase and lowercase), 0-9, and _

2. Cannot start with a number

3. Go strictly distinguishes between uppercase and lowercase

4. Cannot contain spaces

5. The underscore "_" itself is a special identifier in Go, called a blank identifier. It can represent any other identifier, but its corresponding value will be ignored (e.g., ignore a return value), so it can only be used as a placeholder and cannot be used as an identifier

6. Cannot use system reserved words as identifiers, such as: brack, if, etc.

Naming rules for identifiers#

1. Package names should be consistent with the names of directories, preferably meaningful, short, and not conflicting with standard libraries

2. Variable names, function names, and constant names should use camel case

3. If the first letter of a variable name, function name, or constant name is capitalized, it can be accessed by other packages; if the first letter is lowercase, it can only be used within the package (note: it can be simply understood that the first letter capitalized is public, and the first letter lowercase is private)

Keywords and Predefined Identifiers#

Keywords#

Keywords are words with special meanings specified by the program inventor, also known as reserved words. There are a total of 25 keywords in Go
Keywords.png

Predefined Identifiers

There are a total of 36 predefined identifiers, including basic data types and system built-in functions

Predefined Identifiers.png

Operators#

Operators.png

Arithmetic Operators#

package main

import "fmt"

func main() {
	// Use of the + sign
	//1. Positive value
	var n1 int = +8
	fmt.Println(n1)
	//2. Addition operation
	var n2 int = 6 + 3
	fmt.Println(n2)
	//3. String concatenation
	var n3 string = "a" + "b" + "c"
	fmt.Println(n3)

	// Use of the / sign
	fmt.Println(10 / 3)   // The result of the operation between two int types is an int type
	fmt.Println(10.0 / 3) // The result of the operation involving a floating point type is a floating point type

	// Use of the % sign (also known as modulus) Equivalent formula a%b=a-a/b*b
	fmt.Println(10 % 3)   // 1
	fmt.Println(10 % -3)  // -1
	fmt.Println(-10 % 3)  // -1
	fmt.Println(-10 % -3) // 1

	// Use of the ++ increment and -- decrement operators
	var a int = 10
	a++ // Increment by 1
	fmt.Println(a)
	a-- // Decrement by 1
	fmt.Println(a)
}

Assignment Operators#

package main

import "fmt"

func main() {
	var num1 int = 10
	fmt.Println(num1)
	var num2 int = (10+20)%3*8 - 14 // The right side of the equal sign is evaluated before being assigned to the left side
	fmt.Println(num2)

	var num3 int = 20
	num3 += 20 // Equivalent to num3 = num3 + 20
	fmt.Println(num3)

	// Exercise: Swap the values of two variables (similar to swapping the water in two cups, need to use a third empty cup)
	var a int = 6
	var b int = 9
	var x int
	x = a // Temporarily store the value of a in x, so a is in idle state and the next step can be performed
	a = b // Put the value of b into a, so a takes over the value of b, and b is now in idle state
	b = x // Put the value of x back into b, the value of x is passed from a, so it is equivalent to b taking over the value of a, and the swap of values between a and b is achieved
}

Relational Operators#

package main

import "fmt"

func main() {
	fmt.Println(1 == 2) // Check if the two sides are equal, return true if they are equal, false if they are not equal
	fmt.Println(1 != 2) // Check if the two sides are not equal, return true if they are not equal, false if they are equal (opposite of ==)
	// Other relational operators are similar, return true if they meet the conditions, false if they do not meet the conditions
}

Logical Operators#

package main

import "fmt"

func main() {
	// Logical AND: && (true can be understood as true, false can be understood as false,)
	// Both sides must be true for the result to be true, if one side is false, the result is false (one mouse dropping spoils a pot of soup)
	// Characteristics: If the left side is false, the right side does not need to participate in the operation, and the result is false
	fmt.Println(true && true)   // true and true is true, the result is true
	fmt.Println(true && false)  // true and false is false, the result is false
	fmt.Println(false && true)  // false and true is false, the result is false
	fmt.Println(false && false) // false and false is false, the result is false
	// Logical OR: ||
	// Both sides must be false for the result to be false, if one side is true, the result is true
	// Characteristics: If the left side is true, the right side does not need to participate in the operation, and the result is true
	fmt.Println(true && true)   // true and true is true, the result is true
	fmt.Println(true && false)  // true and false is true, the result is true
	fmt.Println(false && true)  // false and true is true, the result is true
	fmt.Println(false && false) // false and false is false, the result is false
	// Logical NOT: ! (takes the opposite result)
	fmt.Println(!true)  // The result is false
	fmt.Println(!false) // The result is true
}

Bitwise Operators#

Only a basic understanding is required!

Other Operators#

&: Returns the storage address of a variable

*: Takes the value corresponding to the pointer variable

*Refer to the pointer section: *[[Go Language Learning Notes/Chapter 4: Complex Data Types]]

Operator Precedence#

Go language has dozens of operators, divided into ten levels:
Operator Precedence.png
==Parentheses can be added to increase precedence()==

Getting User Terminal Input#

func main() {
	// Functionality: Keyboard input of student age, name, score, and whether they are VIP
	// Method 1: Scanln
	var age int
	fmt.Println("Please enter the student's age:")
	// The purpose of passing the address of the variable is that when the value in the address is changed in the Scanln function, the actual age outside is affected
	fmt.Scanln(&age)
	var name string
	fmt.Println("Please enter the student's name:")
	fmt.Scanln(&name)
	var score float32
	fmt.Println("Please enter the student's score:")
	fmt.Scanln(&score)
	var isVIP bool
	fmt.Println("Please enter whether the student is a VIP:")
	fmt.Scanln(&isVIP)
	fmt.Printf("The student's name is: %v, age is: %v, score is: %v, isVIP: %v", name, age, score, isVIP)
	fmt.Println()
	// Method 2: Scanf
	fmt.Println("Please enter the student's age, name, score, and whether they are VIP, separated by spaces")
	fmt.Scanf("%d %s %f %t", &age, &name, &score, &isVIP)
	fmt.Printf("The student's name is: %v, age is: %v, score is: %v, isVIP: %v", name, age, score, isVIP)
}
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.