Latest web development tutorials

Aller structure de la langue

Aller tableau de langue peut stocker le même type de données, mais dans la structure, nous pouvons définir les différents éléments pour les différents types de données.

La structure est composée d'une série d'ensembles de données du même type ou de types différents de données constitués.

Structure représente un enregistrement, tels que l'enregistrement d'une bibliothèque d'enregistrements de livres, chaque livre a les propriétés suivantes:

  • Titre: Titre
  • Auteur: Auteur
  • Sujet: Sujet
  • ID: Livres ID

Définition de la structure

La structure est définie et nécessite l'utilisation de la déclaration de type struct. struct définit un nouveau type de données, la structure comporte un ou plusieurs membres. nom du type instruction définit la structure. Structure de format est le suivant:

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

Une fois que vous avez défini le type de structure, il peut être utilisé pour déclarer des variables, la syntaxe est la suivante:

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

Une structure

Si vous souhaitez accéder à un membre de la structure, utilisez l'opérateur point, le format (.) :. "Un nom de membre de la structure."

Tapez la structure variable en utilisant les définitions de mots clés struct, les exemples sont les suivants:

package main

import "fmt"

type Books struct {
   title string
   author string
   subject string
   book_id int
}

func main() {
   var Book1 Books        /* 声明 Book1 为 Books 类型 */
   var Book2 Books        /* 声明 Book2 为 Books 类型 */

   /* book 1 描述 */
   Book1.title = "Go 语言"
   Book1.author = "www.w3big.com"
   Book1.subject = "Go 语言教程"
   Book1.book_id = 6495407

   /* book 2 描述 */
   Book2.title = "Python 教程"
   Book2.author = "www.w3big.com"
   Book2.subject = "Python 语言教程"
   Book2.book_id = 6495700

   /* 打印 Book1 信息 */
   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)

   /* 打印 Book2 信息 */
   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)
}

Des exemples de la mise en œuvre des résultats d'exploitation ci-dessus comme suit:

Book 1 title : Go 语言
Book 1 author : www.w3big.com
Book 1 subject : Go 语言教程
Book 1 book_id : 6495407
Book 2 title : Python 教程
Book 2 author : www.w3big.com
Book 2 subject : Python 语言教程
Book 2 book_id : 6495700

La structure en tant que paramètre de fonction

Vous pouvez comme d'autres types de données sur le type de structure passée à la fonction en tant que paramètre. Et à titre d'exemple ci-dessus pour accéder à la variable de structure:

package main

import "fmt"

type Books struct {
   title string
   author string
   subject string
   book_id int
}

func main() {
   var Book1 Books        /* 声明 Book1 为 Books 类型 */
   var Book2 Books        /* 声明 Book2 为 Books 类型 */

   /* book 1 描述 */
   Book1.title = "Go 语言"
   Book1.author = "www.w3big.com"
   Book1.subject = "Go 语言教程"
   Book1.book_id = 6495407

   /* book 2 描述 */
   Book2.title = "Python 教程"
   Book2.author = "www.w3big.com"
   Book2.subject = "Python 语言教程"
   Book2.book_id = 6495700

   /* 打印 Book1 信息 */
   printBook(Book1)

   /* 打印 Book2 信息 */
   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);
}

Des exemples de la mise en œuvre des résultats d'exploitation ci-dessus comme suit:

Book title : Go 语言
Book author : www.w3big.com
Book subject : Go 语言教程
Book book_id : 6495407
Book title : Python 教程
Book author : www.w3big.com
Book subject : Python 语言教程
Book book_id : 6495700

Structure pointeur

Vous pouvez définir un pointeur vers une structure similaire à une autre variable de pointeur, le format est le suivant:

var struct_pointer *Books

Défini ci-dessus peuvent répondre pointeur de la mémoire des variables de structure variable. Voir la structure adresse variable, vous pouvez placer une esperluette devant les variables de structure:

struct_pointer = &Book1;

L'utilisation d'un pointeur vers un membre de la structure d'une structure, utilisez l'opérateur ".":

struct_pointer.title;

Nous allons utiliser le pointeur de la structure réécrire l'exemple ci-dessus, le code ci-dessous:

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 描述 */
   Book1.title = "Go 语言"
   Book1.author = "www.w3big.com"
   Book1.subject = "Go 语言教程"
   Book1.book_id = 6495407

   /* book 2 描述 */
   Book2.title = "Python 教程"
   Book2.author = "www.w3big.com"
   Book2.subject = "Python 语言教程"
   Book2.book_id = 6495700

   /* 打印 Book1 信息 */
   printBook(&Book1)

   /* 打印 Book2 信息 */
   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);
}

Des exemples de la mise en œuvre des résultats d'exploitation ci-dessus comme suit:

Book title : Go 语言
Book author : www.w3big.com
Book subject : Go 语言教程
Book book_id : 6495407
Book title : Python 教程
Book author : www.w3big.com
Book subject : Python 语言教程
Book book_id : 6495700