For Golang, the exchange of two numbers is simple, as follows: "I, j = j, I" equals the left and right side of the equal number of expressions, which is the parallel assignment. The assignment is divided into two steps: 1. Evaluates the index expression and the accessor expression for the operand to the left of the equals sign, and evaluates the expression to the right of the equal sign. 2. Assign values let's see what the following code will output. "Func main () {i: = 1s: = []string{" A "," B "," C "}i, s[i-1] = 2," Z "FMT. Printf ("s:%v \ n", s)} ' ' Here output s: [Z,b,c] instead of S: [A,z,c]. This is because the index expression of the slice on the left side of the equal sign i-1 will be evaluated first and then the assignment. The order of calculation and assignment is from left to right. Let's see what the following code will output. "Func Main () {a: = []int{1, 2, 3, 4}defer func (a []int) {fmt. Printf ("A:%v\n", a)} (a) a[0], a[4] = a[1], a[2]} "" is output: ' A: [2 2 3 4]panic:runtime error:index out of Rangegoroutine 1 [Running]: "In the process of assignment, even if the back of the panic is raised, the front will also be assigned a successful value. What do you think this will output? "Func Main () {a: = []int{1, 2, 3, 4}defer func (a []int) {fmt. Printf ("A:%v\n", a)} (a) a[0], a[1] = a[2], a[4]} "" is output: ' A: [1 2 3 4]panic:runtime error:index out of Rangegoroutine 1 [Running]:main.main () "] once again, the first step in the assignment is to calculate the index expression to the left of the equal sign, the address expression, and the expression to the right of the equals sign. Understanding reference to "go concurrent programming book" 91 reads
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service