Go-Strukturen
In Go können Arrays Daten desselben Typs speichern, aber in Strukturen können wir unterschiedliche Datentypen für verschiedene Elemente definieren.
Eine Struktur ist eine Datensammlung, die aus einer Reihe von Daten desselben Typs oder unterschiedlicher Typen besteht.
Eine Struktur repräsentiert einen Datensatz, wie zum Beispiel das Speichern von Bibliotheksbuchdatensätzen. Jedes Buch hat die folgenden Eigenschaften:
- Titel: Titel
- Autor: Autor
- Thema: Thema
- ID: Buch-ID
Struktur definieren
Zum Definieren einer Struktur sind die Anweisungen type und struct erforderlich. Die struct-Anweisung definiert einen neuen Datentyp, und die Struktur hat ein oder mehrere Mitglieder. Die type-Anweisung legt den Namen der Struktur fest. Das Format einer Struktur ist wie folgt:
type struct_variable_type struct {
member definition
member definition
...
member definition
}
Sobald ein Strukturtyp definiert ist, kann er für Variablendeklarationen verwendet werden. Das Syntaxformat ist wie folgt:
variable_name := structure_variable_type {value1, value2...valuen}
oder
variable_name := structure_variable_type { key1: value1, key2: value2..., keyn: valuen}
Beispiel wie folgt:
Beispiel
import "fmt"
type Books struct {
title string
author string
subject string
book_id int
}
func main() {
// Ein neues struct erstellen
fmt.Println(Books{"Go language", "www.example.com", "Go language tutorial", 6495407})
// Sie können auch das key => value-Format verwenden
fmt.Println(Books{title: "Go language", author: "www.example.com", subject: "Go language tutorial", book_id: 6495407})
// Ignorierte Felder sind 0 oder leer
fmt.Println(Books{title: "Go language", author: "www.example.com"})
}
Das Ausgabeergebnis ist:
{Go 语言 www.example.com Go 语言教程 6495407}
{Go 语言 www.example.com Go 语言教程 6495407}
{Go 语言 www.example.com 0}
Zugriff auf Strukturmitglieder
Um auf Strukturmitglieder zuzugreifen, müssen Sie den Punktoperator (.) verwenden..Operator, im Format:
struct.member name
Variablen vom Strukturtyp werden mit dem Schlüsselwort struct definiert, wie im folgenden Beispiel:
Beispiel
import "fmt"
type Books struct {
title string
author string
subject string
book_id int
}
func main() {
var Book1 Books /* Book1 als Typ Books deklarieren */
var Book2 Books /* Book2 als Typ Books deklarieren */
/* Beschreibung von Buch 1 */
Book1.title = "Go-Sprache"
Book1.author = "www.example.com"
Book1.subject = "Go-Sprache-Tutorial"
Book1.book_id = 6495407
/* Beschreibung von Buch 2 */
Book2.title = Python-Tutorial
Book2.author = "www.example.com"
Book2.subject = Python-Sprachtutorial
Book2.book_id = 6495700
/* Book1-Informationen ausgeben */
fmt.Printf( "Buch 1 Titel : %s\n", Book1.title)
fmt.Printf( "Buch 1 Autor : %s\n", Book1.author)
fmt.Printf( "Buch 1 Thema : %s\n", Book1.subject)
fmt.Printf( "Buch 1 book_id : %d\n", Book1.book_id)
/* Book2-Informationen ausgeben */
fmt.Printf( "Buch 2 Titel : %s\n", Book2.title)
fmt.Printf( "Buch 2 Autor : %s\n", Book2.author)
fmt.Printf( "Buch 2 Thema : %s\n", Book2.subject)
fmt.Printf( "Buch 2 book_id : %d\n", Book2.book_id)
}
Das Ausführungsergebnis des obigen Beispiels ist:
Book 1 title : Go 语言 Book 1 author : www.example.com Book 1 subject : Go 语言教程 Book 1 book_id : 6495407 Book 2 title : Python 教程 Book 2 author : www.example.com Book 2 subject : Python 语言教程 Book 2 book_id : 6495700
Strukturen als Funktionsparameter
Sie können einen Struct-Typ genauso wie andere Datentypen als Parameter an eine Funktion übergeben und auf die Struct-Variablen wie in den obigen Beispielen zugreifen:
Beispiel
import "fmt"
type Books struct {
title string
author string
subject string
book_id int
}
func main() {
var Book1 Books /* Book1 als Typ Books deklarieren */
var Book2 Books /* Book2 als Typ Books deklarieren */
/* Beschreibung von Buch 1 */
Book1.title = "Go-Sprache"
Book1.author = "www.example.com"
Book1.subject = "Go-Sprache-Tutorial"
Book1.book_id = 6495407
/* Beschreibung von Buch 2 */
Book2.title = Python-Tutorial
Book2.author = "www.example.com"
Book2.subject = Python-Sprachtutorial
Book2.book_id = 6495700
/* Book1-Informationen ausgeben */
printBook(Book1)
/* Book2-Informationen ausgeben */
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)
}
Das Ausführungsergebnis des obigen Beispiels ist:
Book title : Go 语言 Book author : www.example.com Book subject : Go 语言教程 Book book_id : 6495407 Book title : Python 教程 Book author : www.example.com Book subject : Python 语言教程 Book book_id : 6495700
Struct-Zeiger
Sie können einen Zeiger auf ein Struct ähnlich wie andere Zeigervariablen definieren. Das Format ist wie folgt:
var struct_pointer *Books
Die oben definierte Zeigervariable kann die Adresse einer Struct-Variablen speichern. Um die Adresse einer Struct-Variablen anzuzeigen, können Sie das &-Symbol vor die Struct-Variable setzen:
struct_pointer = &Book1
Verwenden Sie den "."-Operator, um über einen Struct-Zeiger auf Struct-Member zuzugreifen:
struct_pointer.title
Als Nächstes verwenden wir einen Struct-Zeiger, um das obige Beispiel neu zu schreiben. Der Code sieht wie folgt aus:
Beispiel
import "fmt"
type Books struct {
title string
author string
subject string
book_id int
}
func main() {
var Book1 Books /* Book1 als Typ Books deklarieren */
var Book2 Books /* Book2 als Typ Books deklarieren */
/* Beschreibung von Buch 1 */
Book1.title = "Go-Sprache"
Book1.author = "www.example.com"
Book1.subject = "Go-Sprache-Tutorial"
Book1.book_id = 6495407
/* Beschreibung von Buch 2 */
Book2.title = Python-Tutorial
Book2.author = "www.example.com"
Book2.subject = Python-Sprachtutorial
Book2.book_id = 6495700
/* Book1-Informationen ausgeben */
printBook(&Book1)
/* Book2-Informationen ausgeben */
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)
}
Das Ausführungsergebnis des obigen Beispiels ist:
Book title : Go 语言 Book author : www.example.com Book subject : Go 语言教程 Book book_id : 6495407 Book title : Python 教程 Book author : www.example.com Book subject : Python 语言教程 Book book_id : 6495700Weitere Erweiterungen