Go - Opérateurs divers

Il existe quelques autres opérateurs importants pris en charge par Go Language, notamment sizeof et ?:.

Opérateur La description Exemple
& Renvoie l'adresse d'une variable. &une; fournit l'adresse réelle de la variable.
* Pointeur vers une variable. *une; fournit un pointeur vers une variable.

Exemple

Essayez l'exemple suivant pour comprendre tous les opérateurs divers disponibles dans le langage de programmation 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);
}

Lorsque vous compilez et exécutez le programme ci-dessus, il produit le résultat suivant -

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.