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
廣告
© . All rights reserved.