Offer to come, dig friends take it! I am participating in the 2022 Spring Recruit Punch card activity. Click here for details.
Spring Recruit punch card day 7 chapter 10.
Today we will continue to update the Go language learning record.
The pointer pointer and switch are two points of knowledge that are not directly related to each other. The reason why they are included in this article is that they are often ignored in the process of learning and using.
pointer
The go language does not have pointer operations, only two symbols: & get memory address * value based on memory address
application
n := 18
/ / get the address
fmt.Println(&n)
fmt.Println(*&n)
Copy the code
Print result:
Example Query the type of the memory address
p := &n
// Values by address
fmt.Printf("%T\n", p) // Prints *int, which is a pointer of type int
m := *p
fmt.Println(m) // Values by address
Copy the code
Print result:
We find that we print: *int, which is a pointer of type int
nil pointer
Var a1 *int //nil > var a2 = new(int) fmt.println (a2) // Memory address 0xC000108010 fmt.println (*a2) // Return 0 *a2 = 100 based on the memory address. // Set the memory address to fmt.Println(*a2) //100Copy the code
Print result:
conclusion
- To obtain a pointer to a variable, perform an address fetch operation (&)
- The value of a pointer variable is a pointer address (memory address)
- The value of a pointer variable can be obtained by performing the operation (*) on the pointer variable to obtain the value of the original variable, that is, by the memory address.
switch
We tend to use if judgment, switch can simplify our if judgment.
The function of switch is the same as that of if, which is to make conditional judgment. The reason why switch is introduced is to simplify our if judgment and make the code more readable.
Better readability
Here’s an example:
To judge the name of a finger:
finger :=2
if finger==1 {
fmt.Println("Thumb")}else if finger==2 {
fmt.Println("Finger")}else if finger==5 {
fmt.Println("Little finger")}else {
fmt.Println("Invalid")}Copy the code
Switch Determines the finger name
finger := 2
switch finger {
case 1:
fmt.Println("Thumb")
case 2:
fmt.Println("Finger")
case 5:
fmt.Println("Little finger")
default:
fmt.Println("Invalid")}Copy the code
By contrast, pole silhouette: Switch Case is more readable.
Multiple parameters are supported after case
For example: the odd and even judgment
switch n := 3; n {
case 1.3.5.7.9:
fmt.Println("Odd")
case 2.4.6.8.10:
fmt.Println("Even")}Copy the code
Add judgment after case
Here’s an example:
age := 29
switch {
case age < 18:
fmt.Println("Study Z.")
case age > 18 && age < 60:
fmt.Println("Go to work.")
case age > 60:
fmt.Println("I hope I don't have to go back to work, haha.")
default:
fmt.Println(age)
}
Copy the code
Note: There is no need to pass an argument after switch when adding a judgment after case, otherwise an error will be reported: type mismatch.
fallthrough
Use advice
Within a switch block, each case does not need to declare a break to terminate. Fallthrough is used if sequential execution is desired. A switch block must contain a default statement at the end, even if it has no code at all.
package main
import "fmt"
func main() {
switch {
case false:
fmt.Println("false1")
fallthrough
case true:
fmt.Println("true1")
fallthrough
case false:
fmt.Println("false2")
fallthrough
case true:
fmt.Println("true2")
case false:
fmt.Println("false3")
fallthrough
default:
fmt.Println("default case")
}
}
Copy the code
conclusion
I believe you will have a deeper understanding of Pointers in go after reading this article
As for switch, as long as we have this concept in mind: Switch works like if, and when we realize that we need to write more than one IF judgment, it is often good practice to implement switch instead.
Any other questions that you think are important, but might be overlooked by beginners, please leave them in the comments section.
The last
Thanks for reading and welcome to like, favorites,coin(attention)!!