package main import"fmt" funcmain(){ var num1 int = 10 fmt.Println(num1) var num2 int = (10 + 20) % 3 + 3 - 7//=右侧的值运算清楚后,再赋值给=的左侧 fmt.Println(num2) var num3 int = 10 num3 += 20//等价num3 = num3 + 20; fmt.Println(num3) }
【4】练习:
1 2 3 4 5 6 7 8 9 10 11
//练习:交换两个数的值并输出结果: var a int = 8 var b int = 4 fmt.Printf("a = %v,b = %v",a,b) //交换: //引入一个中间变量: var t int t = a a = b b = t fmt.Printf("a = %v,b = %v",a,b)
package main import"fmt" funcmain(){ //定义一个变量: var age int = 18 fmt.Println("age对应的存储空间的地址为:",&age)//age对应的存储空间的地址为: 0xc0000100b0 var ptr *int = &age fmt.Println(ptr) fmt.Println("ptr这个指针指向的具体数值为:",*ptr) }