Latest web development tutorials

Zum Sprachstruktur

Gehen Sprach Array speichern kann die gleiche Art von Daten, aber in der Struktur, können wir verschiedene Elemente für verschiedene Datentypen definieren.

Struktur besteht aus einer Serie von Datensätzen von der gleichen Art oder von unterschiedlichen Arten von Daten gebildet wird.

Struktur stellt eine Aufzeichnung, wie eine Aufzeichnung Bibliothek der Bücher speichern, jedes Buch hat die folgenden Eigenschaften:

  • Titel: Titel
  • Autor: Autor
  • Thema: Thema
  • ID: Bücher ID

Definition der Struktur

Struktur definiert und erfordert die Verwendung von Typ struct Anweisung. struct einen neuen Datentyp zu definieren, wobei die Struktur ein oder mehrere Mitglieder. Typname Anweisung setzt die Struktur. Formatstruktur ist wie folgt:

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

Sobald Sie die Art der Struktur definiert haben, kann es verwendet werden, um Variablen zu deklarieren, die Syntax ist wie folgt:

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

Auf eine Struktur,

Wenn Sie ein Strukturelement zugreifen möchten, verwenden Sie den Punkt-Operator, das Format (.) :. "Ein Strukturelement Name."

Geben Sie variable Struktur, die die Schlüsselwort struct-Definitionen, Beispiele sind wie folgt:

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

Beispiele für die Umsetzung der oben genannten operativen Ergebnisse wie folgt:

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

Die Struktur als Funktionsparameter

Sie können auch andere Datentypen wie dem Typ der Struktur, der Funktion als Parameter übergeben. Und oben beispielhaft die Struktur Variable zuzugreifen:

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

Beispiele für die Umsetzung der oben genannten operativen Ergebnisse wie folgt:

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

Strukturzeiger

Sie können einen Zeiger auf eine Struktur, ähnlich zu anderen Zeigervariable definieren, ist das Format wie folgt:

var struct_pointer *Books

Oben definiert, können die Speicherzeigervariable Strukturvariablen adressieren. Siehe Struktur variable Adresse können Sie ein kaufmännisches vor den Strukturvariablen platzieren:

struct_pointer = &Book1;

Mit einem Zeiger auf eine Struktur Element einer Struktur, verwenden Sie den Operator ".":

struct_pointer.title;

Lassen Sie uns die Struktur Zeiger umschreiben das obige Beispiel verwenden, den Code unten:

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

Beispiele für die Umsetzung der oben genannten operativen Ergebnisse wie folgt:

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