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