W języku Go struktury umożliwiają grupowanie elementów różnych typów w jedną jednostkę, co jest przydatne przy modelowaniu rzeczywistych jednostek. Anonimowe struktury w Golangu to tymczasowe struktury bez nazw, używane do jednorazowych celów, natomiast pola anonimowe umożliwiają osadzanie nienazwanych pól.

Na przykład:
package main
import "fmt"
// struct học sinh với cấu trúc và trường ẩn danh
type Student struct {
struct { // Cấu trúc bên trong ẩn danh cho thông tin cá nhân
name string
enrollment int
}
GPA float64 // Trường chuẩn
}
func main() {
student := Student{
struct {
name string
enrollment int
}{
name: "A",
enrollment: 12345,
},
GPA: 3.8,
}
fmt.Println("Name:", student.name)
fmt.Println("Enrollment:", student.enrollment)
fmt.Println("GPA:", student.GPA)
}
Składnia:
variable := struct {
field1 dataType1
field2 dataType2 # Cấu trúc ẩn danh
// Trường bổ sung khi cần
}{value1, value2}
type StructName struct {
dataType1
dataType2 # Trường ẩn danh
// Trường ẩn danh bổ sung
}
Anonimowe struktury w Go
Anonimowe struktury w języku Go są definiowane bez nazwy i są przydatne do tworzenia tymczasowych, jednorazowych struktur. Oto składnia i przykład kodu.
Składnia:
variable := struct {
field1 dataType1
field2 dataType2
// Các trường bổ sung khi cần
}{value1, value2}
Na przykład:
package main
import "fmt"
// Cấu trúc sinh viên với cấu trúc bên trong ẩn danh cho thông tin cá nhân
type Student struct {
personalDetails struct { // Cấu trúc ẩn danh bên trong cho thông tin cá nhân
name string
enrollment int
}
GPA float64 // Trường chuẩn
}
func main() {
// Khởi tạo cấu trúc bên trong cho student
student := Student{
personalDetails: struct {
name string
enrollment int
}{
name: "A",
enrollment: 12345,
},
GPA: 3.8,
}
// Hiện giá trị
fmt.Println("Name:", student.personalDetails.name)
fmt.Println("Enrollment:", student.personalDetails.enrollment)
fmt.Println("GPA:", student.GPA)
}
Wynik:
Name: A
Enrollment: 12345
GPA: 3.8
Ten kod definiuje strukturę Studenta zawierającą anonimową strukturę personalDetails , przechowującą imię i nazwisko oraz informacje rejestracyjne. Następnie zainicjuj studenta wartościami dla tych pól i wydrukuj je.
Pola anonimowe
Pola anonimowe w języku Go pozwalają na definiowanie pól bez podawania ich nazw, określane są jedynie ich typy. Jest to przydatne, gdy pola naturalnie następują po nazwie typu.
Składnia
type StructName struct {
dataType1
dataType2
// Additional anonymous fields
}
Na przykład:
package main
import "fmt"
// Cấu trúc học sinh bằng các trường ẩn danh
type Student struct {
int // Số đăng ký (trường ẩn danh)
string // Tên trường ẩn danh
float64 // GPA (trường ẩn danh)
}
func main() {
// Khởi tạo struct học sinh với các trường ẩn danh
student := Student{12345, "A", 3.8}
// Hiện giá trị
fmt.Println("Enrollment:", student.int)
fmt.Println("Name:", student.string)
fmt.Println("GPA:", student.float64)
}
Wynik:
Enrollment: 12345
Name: A
GPA: 3.8
W tym przypadku typy danych ( int, string, float64 ) pełnią funkcję nazw pól, więc dostęp do wartości zależy od typów.
Ważne punkty do zapamiętania o polach anonimowych w Golang
1. Wymaganie unikalne: Nie można używać dwóch pól tego samego typu w jednej strukturze. Na przykład:
type InvalidStudent struct {
int
int // Error: duplicate type
}
2. Łączenie pól nazwanych i anonimowych: W strukturze można łączyć pola anonimowe i nazwane.
type Student struct {
id int // Named field
int // Anonymous field
}