- Go 教程
- Go - 首頁
- Go - 概覽
- Go - 環境設定
- Go - 程式結構
- Go - 基本語法
- Go - 資料型別
- Go - 變數
- Go - 常量
- Go - 運算子
- Go - 決策制定
- Go - 迴圈
- Go - 函式
- Go - 作用域規則
- Go - 字串
- Go - 陣列
- Go - 指標
- Go - 結構
- Go - 切片
- Go - 範圍
- Go - 對映
- Go - 遞迴
- Go - 型別轉換
- Go - 介面
- Go - 錯誤處理
- Go 實用資源
- Go - 問題和答案
- Go - 快速指南
- Go - 實用資源
- Go - 討論
Go - 其他運算子
Go 語言支援其他幾個重要運算子,包括 sizeof 和 ?:.
| 運算子 | 說明 | 示例 |
|---|---|---|
| & | 返回變數的地址。 | &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.
go_operators.htm
廣告