البنية أو الهيكل في Golang هو نوع محدد من قبل المستخدم يسمح لنا بإنشاء مجموعة من العناصر من أنواع مختلفة في وحدة واحدة. يمكن تمثيل أي كيان في العالم الحقيقي يحتوي على مجموعة من الخصائص أو الحقول على هيئة هيكل.

كيفية استخدام الهياكل المتداخلة في Golang
تسمح لغة Go بالهياكل المتداخلة. يُطلق على الهيكل الذي يكون حقلاً لهيكل آخر اسم الهيكل المتداخل. بعبارة أخرى، يُطلق على الهيكل الموجود داخل هيكل آخر اسم الهيكل المتداخل. بناء الجملة:
type struct_name_1 struct{
// Các trường
}
type struct_name_2 struct{
variable_name struct_name_1
}
فكر في المثال التالي لفهم البنية المتداخلة في Golang:
مثال 1:
// Chương trình Go minh hoạc
// cấu trúc lồng nhau
package main
import "fmt"
// Tạo cấu trúc
type Author struct {
name string
branch string
year int
}
// Tạo cấu trúc lòng nhau
type HR struct {
// cấu trúc là một trường
details Author
}
func main() {
// Khởi tạo các trường
// của cấu trúc
result := HR{
details: Author{"Sona", "ECE", 2013},
}
// Hiện giá trị
fmt.Println("\nDetails of Author")
fmt.Println(result)
}
نتيجة:
Details of Author
{{Sona ECE 2013}}
مثال 2:
// Chương trình Golang minh họa
// cấu trúc lồng nhau
package main
import "fmt"
// Tạo cấu trúc
type Student struct {
name string
branch string
year int
}
// Tạo cấu trúc lồng nhau
type Teacher struct {
name string
subject string
exp int
details Student
}
func main() {
// Khởi tạo các trường
// của cấu trúc
result := Teacher{
name: "Suman",
subject: "Java",
exp: 5,
details: Student{"Bongo", "CSE", 2},
}
// Hiện giá trị
fmt.Println("Details of the Teacher")
fmt.Println("Teacher's name: ", result.name)
fmt.Println("Subject: ", result.subject)
fmt.Println("Experience: ", result.exp)
fmt.Println("\nDetails of Student")
fmt.Println("Student's name: ", result.details.name)
fmt.Println("Student's branch name: ", result.details.branch)
fmt.Println("Year: ", result.details.year)
}
نتيجة:
Details of the Teacher
Teacher's name: Suman
Subject: Java
Experience: 5
Details of Student
Student's name: Bongo
Student's branch name: CSE
Year: 2
مثال 3:
في Go، يمكن أن يحتوي الهيكل على حقول تعتبر في حد ذاتها هياكل، تسمى الهياكل المتداخلة. فيما يلي مثال لهيكل يحتوي على هيكل متداخل:
package main
import (
"fmt"
)
type Address struct {
Street string
City string
State string
PostalCode string
}
type Person struct {
FirstName string
LastName string
Age int
Address Address
}
func main() {
p := Person{
FirstName: "John",
LastName: "Doe",
Age: 30,
Address: Address{
Street: "123 Main St",
City: "Anytown",
State: "CA",
PostalCode: "12345",
},
}
fmt.Println(p.FirstName, p.LastName)
fmt.Println("Age:", p.Age)
fmt.Println("Address:")
fmt.Println("Street:", p.Address.Street)
fmt.Println("City:", p.Address.City)
fmt.Println("State:", p.Address.State)
fmt.Println("Postal Code:", p.Address.PostalCode)
}
نتيجة:
John Doe
Age: 30
Address:
Street: 123 Main St
City: Anytown
State: CA
Postal Code: 12345
هنا، نقوم بتعريف نوعين من الهياكل: الشخص والعنوان. لدى الشخص حقل هيكل متداخل يسمى العنوان. في الوظيفة الرئيسية، نقوم بإنشاء مثيل شخص جديد مع حقل العنوان. نقوم بعد ذلك بطباعة قيم الحقول المختلفة لهياكل الشخص والعنوان باستخدام تدوين النقاط للوصول إلى الحقول المتداخلة.