Latest web development tutorials

Vai struttura del linguaggio

Vai matrice lingua può memorizzare lo stesso tipo di dati, ma nella struttura, possiamo definire articoli differenti per i diversi tipi di dati.

La struttura è composta da una serie di gruppi di dati dello stesso tipo o di tipi diversi di dati costituiti.

Struttura rappresenta un record, come il salvataggio di una discoteca di libri, ogni prodotto ha le seguenti proprietà:

  • Titolo: Titolo
  • Autore: Autore
  • Oggetto: Subject
  • ID: Libri ID

Definizione della struttura

Struttura è definito e richiede l'uso di dichiarazione tipo struct. struct definirà un nuovo tipo di dati, la struttura ha uno o più membri. nome del tipo di istruzione imposta la struttura. Struttura definizione è la seguente:

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

Dopo aver definito il tipo di struttura, può essere utilizzato per dichiarare le variabili, la sintassi è la seguente:

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

Per una struttura

Se si desidera accedere a un membro della struttura, utilizzare l'operatore punto, il formato (.) :. "Un nome membro della struttura."

Tipo di struttura variabile utilizzando le definizioni struct di parole chiave, gli esempi sono i seguenti:

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)
}

Esempi di applicazione dei risultati operativi sopra come segue:

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 struttura come parametro funzione

È possibile, come altri tipi di dati per il tipo di struttura passato alla funzione come parametro. E a titolo di esempio sopra per accedere alla variabile struttura:

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);
}

Esempi di applicazione dei risultati operativi sopra come segue:

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

puntatore struttura

È possibile definire un puntatore che punta a una struttura simile ad altre variabili puntatore, il formato è il seguente:

var struct_pointer *Books

Definito ciò può rivolgersi puntatore di memoria variabili di struttura variabile. Vedere la struttura indirizzo variabile, è possibile inserire una e commerciale davanti alle variabili di struttura:

struct_pointer = &Book1;

Utilizzando un puntatore a un membro di una struttura di una struttura, utilizzare l'operatore "".:

struct_pointer.title;

Usiamo il puntatore struttura di riscrivere l'esempio precedente, il codice qui sotto:

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);
}

Esempi di applicazione dei risultati operativi sopra come segue:

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