GO 三、指針、控制語句
指針、pointer
& 取變量地址
* 訪問目標對象
默認值是nil不是null
package main
import "fmt"
func main() {
b := 6
var b_ptr *int // *int is used delcare variable
// b_ptr to be a pointer to an int
b_ptr = &b // b_ptr is assigned value that is the address
// of where variable b is stored
// Shorhand for the above two lines is:
// b_ptr := &b
fmt.Printf("address of b_ptr: %p\n", b_ptr)
// We can use *b_ptr get the value that is stored
// at address b_ptr, or dereference the pointer
fmt.Printf("value stored at b_ptr: %d\n", *b_ptr)
a:=1
var p *int = &a
}
條件語句
if a :=1 ; a>1 {
}
a := 1
for {
a++
if a>3{
break
}
}
b :=1
for b<=3{
b++
}
for i :=0 ; i<3 ; i++{
}
a := 1
switch(a){
case 0:
fmt.Println("a is 0")
case 1:
fmt.Println("a is 1")
default:
fmt.Println("default value")
}