A structure is a collection of data fields with declared data types. Golang can declare and create its data types by combining one or more types, including both built-in and user-defined types.
package main import "fmt" type student struct { Name string class string Roll_No int Marks int } func main() { var s1 = student{"Sandeep", "X", 2001, 98} var s2 student s2.Name = "Sonu" s2.class = "IX" s2.Roll_No = 2039 s2.Marks = 56 fmt.Println(s1) fmt.Println(s2) }
- Accessing Structure members
package main import "fmt" type Books struct { title string author string subject string book-id int } func main() { var Book1 Books var Book2 Books Book1.title = "Go Programing" Book1.author = "Kreston" Book1.subject = "Go programing tutorial" Book1.book-id = 398312389 Book2.title = "Python Programing" Book2.author = "Hazera Ali" Book2.subject = "Python programing tutorial" Book2.book-id = 392342334 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 :%s\n", Book1.book-id) 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 :", Book2.book-id) }