還有一些其他重要的運算符包括sizeof
和? :
,在Go語言中也是支持的。
運算符 | 描述 | 示例 |
---|---|---|
& | 返回變數的地址 | &a 將給出變數a 的實際地址。 |
* | 指向變數的指針 | *a 是指向變數a 的指針。 |
示例
嘗試以下示例來瞭解Go編程語言中提供的其他運算符:
package main
import "fmt"
func main() {
var a int = 4
var b int32
var c float32
var ptr *int
/* example of type operator */
fmt.Printf("Line 1 - Type of variable a = %T\n", a );
fmt.Printf("Line 2 - Type of variable b = %T\n", b );
fmt.Printf("Line 3 - Type of variable c= %T\n", c );
/* example of & and * operators */
ptr = &a /* 'ptr' now contains the address of 'a'*/
fmt.Printf("value of a is %d\n", a);
fmt.Printf("*ptr is %d.\n", *ptr);
}
當編譯和執行上面程式,它產生以下結果:
Line 1 - Type of variable a = int
Line 2 - Type of variable b = int32
Line 3 - Type of variable c= float32
value of a is 4
*ptr is 4.