Aller - Structures

Les tableaux Go vous permettent de définir des variables pouvant contenir plusieurs éléments de données du même type. Structure est un autre type de données défini par l'utilisateur disponible dans la programmation Go, qui vous permet de combiner des éléments de données de différents types.

Les structures sont utilisées pour représenter un enregistrement. Supposons que vous souhaitiez garder une trace des livres dans une bibliothèque. Vous voudrez peut-être suivre les attributs suivants de chaque livre -

  • Title
  • Author
  • Subject
  • ID du livre

Dans un tel scénario, les structures sont très utiles.

Définition d'une structure

Pour définir une structure, vous devez utiliser type et structdéclarations. L'instruction struct définit un nouveau type de données, avec plusieurs membres pour votre programme. L'instruction de type lie un nom au type qui est struct dans notre cas. Le format de l'instruction struct est le suivant -

type struct_variable_type struct {
   member definition;
   member definition;
   ...
   member definition;
}

Une fois qu'un type de structure est défini, il peut être utilisé pour déclarer des variables de ce type à l'aide de la syntaxe suivante.

variable_name := structure_variable_type {value1, value2...valuen}

Accès aux membres de la structure

Pour accéder à n'importe quel membre d'une structure, nous utilisons le member access operator (.).L'opérateur d'accès au membre est codé comme un point entre le nom de la variable de structure et le membre de la structure auquel nous souhaitons accéder. Vous utiliseriezstructmot-clé pour définir des variables de type structure. L'exemple suivant explique comment utiliser une structure -

package main

import "fmt"

type Books struct {
   title string
   author string
   subject string
   book_id int
}
func main() {
   var Book1 Books    /* Declare Book1 of type Book */
   var Book2 Books    /* Declare Book2 of type Book */
 
   /* book 1 specification */
   Book1.title = "Go Programming"
   Book1.author = "Mahesh Kumar"
   Book1.subject = "Go Programming Tutorial"
   Book1.book_id = 6495407

   /* book 2 specification */
   Book2.title = "Telecom Billing"
   Book2.author = "Zara Ali"
   Book2.subject = "Telecom Billing Tutorial"
   Book2.book_id = 6495700
 
   /* print Book1 info */
   fmt.Printf( "Book 1 title : %s\n", Book1.title)
   fmt.Printf( "Book 1 author : %s\n", Book1.author)
   fmt.Printf( "Book 1 subject : %s\n", Book1.subject)
   fmt.Printf( "Book 1 book_id : %d\n", Book1.book_id)

   /* print Book2 info */
   fmt.Printf( "Book 2 title : %s\n", Book2.title)
   fmt.Printf( "Book 2 author : %s\n", Book2.author)
   fmt.Printf( "Book 2 subject : %s\n", Book2.subject)
   fmt.Printf( "Book 2 book_id : %d\n", Book2.book_id)
}

Lorsque le code ci-dessus est compilé et exécuté, il produit le résultat suivant -

Book 1 title      : Go Programming
Book 1 author     : Mahesh Kumar
Book 1 subject    : Go Programming Tutorial
Book 1 book_id    : 6495407
Book 2 title      : Telecom Billing
Book 2 author     : Zara Ali
Book 2 subject    : Telecom Billing Tutorial
Book 2 book_id    : 6495700

Structures comme arguments de fonction

Vous pouvez transmettre une structure en tant qu'argument de fonction de manière très similaire à celle de toute autre variable ou pointeur. Vous accéderiez aux variables de structure de la même manière que vous l'avez fait dans l'exemple ci-dessus -

package main

import "fmt"

type Books struct {
   title string
   author string
   subject string
   book_id int
}
func main() {
   var Book1 Books    /* Declare Book1 of type Book */
   var Book2 Books    /* Declare Book2 of type Book */
 
   /* book 1 specification */
   Book1.title = "Go Programming"
   Book1.author = "Mahesh Kumar"
   Book1.subject = "Go Programming Tutorial"
   Book1.book_id = 6495407

   /* book 2 specification */
   Book2.title = "Telecom Billing"
   Book2.author = "Zara Ali"
   Book2.subject = "Telecom Billing Tutorial"
   Book2.book_id = 6495700
 
   /* print Book1 info */
   printBook(Book1)

   /* print Book2 info */
   printBook(Book2)
}
func printBook( book Books ) {
   fmt.Printf( "Book title : %s\n", book.title);
   fmt.Printf( "Book author : %s\n", book.author);
   fmt.Printf( "Book subject : %s\n", book.subject);
   fmt.Printf( "Book book_id : %d\n", book.book_id);
}

Lorsque le code ci-dessus est compilé et exécuté, il produit le résultat suivant -

Book title     : Go Programming
Book author    : Mahesh Kumar
Book subject   : Go Programming Tutorial
Book book_id   : 6495407
Book title     : Telecom Billing
Book author    : Zara Ali
Book subject   : Telecom Billing Tutorial
Book book_id   : 6495700

Pointeurs vers des structures

Vous pouvez définir des pointeurs vers des structures de la même manière que vous définissez un pointeur vers toute autre variable comme suit -

var struct_pointer *Books

Vous pouvez maintenant stocker l'adresse d'une variable de structure dans la variable de pointeur définie ci-dessus. Pour trouver l'adresse d'une variable de structure, placez l'opérateur & avant le nom de la structure comme suit -

struct_pointer = &Book1;

Pour accéder aux membres d'une structure à l'aide d'un pointeur vers cette structure, vous devez utiliser le "." opérateur comme suit -

struct_pointer.title;

Réécrivons l'exemple ci-dessus en utilisant le pointeur de structure -

package main

import "fmt"

type Books struct {
   title string
   author string
   subject string
   book_id int
}
func main() {
   var Book1 Books   /* Declare Book1 of type Book */
   var Book2 Books   /* Declare Book2 of type Book */
 
   /* book 1 specification */
   Book1.title = "Go Programming"
   Book1.author = "Mahesh Kumar"
   Book1.subject = "Go Programming Tutorial"
   Book1.book_id = 6495407

   /* book 2 specification */
   Book2.title = "Telecom Billing"
   Book2.author = "Zara Ali"
   Book2.subject = "Telecom Billing Tutorial"
   Book2.book_id = 6495700
 
   /* print Book1 info */
   printBook(&Book1)

   /* print Book2 info */
   printBook(&Book2)
}
func printBook( book *Books ) {
   fmt.Printf( "Book title : %s\n", book.title);
   fmt.Printf( "Book author : %s\n", book.author);
   fmt.Printf( "Book subject : %s\n", book.subject);
   fmt.Printf( "Book book_id : %d\n", book.book_id);
}

Lorsque le code ci-dessus est compilé et exécuté, il produit le résultat suivant -

Book title     : Go Programming
Book author    : Mahesh Kumar
Book subject   : Go Programming Tutorial
Book book_id   : 6495407
Book title     : Telecom Billing
Book author    : Zara Ali
Book subject   : Telecom Billing Tutorial
Book book_id   : 6495700