Go语言其它运算符示例

还有一些其他重要的运算符包括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.

上一篇: Go语言运算符 下一篇: Go语言条件和决策