In Golang , le strutture (o struct) consentono di raggruppare elementi di tipi diversi in un'unica unità, il che è utile per modellare entità del mondo reale. Le strutture anonime in Golang sono strutture temporanee senza nome, utilizzate per scopi una tantum, mentre i campi anonimi consentono l'incorporamento di campi senza nome.

Per esempio:
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)
}
Sintassi:
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
}
Strutture anonime in Go
Le strutture anonime in Go sono definite senza nome e sono utili per creare strutture temporanee e monouso. Ecco la sintassi e l'esempio di codice.
Sintassi:
variable := struct {
field1 dataType1
field2 dataType2
// Các trường bổ sung khi cần
}{value1, value2}
Per esempio:
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)
}
Risultato:
Name: A
Enrollment: 12345
GPA: 3.8
Questo codice definisce una struttura Student con al suo interno una struttura personalDetails anonima , che memorizza il nome e le informazioni di registrazione. Quindi inizializzare lo studente con i valori per questi campi e stamparli.
Campi anonimi
I campi anonimi in Go consentono di definire campi senza nomi espliciti, di cui vengono specificati solo i tipi. Ciò è utile quando i campi seguono naturalmente il nome del tipo.
Sintassi
type StructName struct {
dataType1
dataType2
// Additional anonymous fields
}
Per esempio:
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)
}
Risultato:
Enrollment: 12345
Name: A
GPA: 3.8
Qui i tipi di dati ( int, string, float64 ) fungono da nomi di campo, quindi l'accesso ai valori dipende dai tipi.
Punti importanti da ricordare sui campi anonimi in Golang
1. Requisito univoco: non è possibile utilizzare due campi dello stesso tipo in una struttura. Per esempio:
type InvalidStudent struct {
int
int // Error: duplicate type
}
2. Combinazione di campi denominati e anonimi: è possibile combinare campi anonimi e denominati in una struttura.
type Student struct {
id int // Named field
int // Anonymous field
}