0
0
Goprogramming~30 mins

Struct usage patterns in Go - Mini Project: Build & Apply

Choose your learning style9 modes available
Struct usage patterns
📖 Scenario: You are building a simple program to manage information about books in a library. Each book has a title, author, and number of pages.
🎯 Goal: Create a Go program that uses a struct to store book information, then create an instance of the struct, update a field, and print the book details.
📋 What You'll Learn
Define a struct named Book with fields Title (string), Author (string), and Pages (int).
Create a variable myBook of type Book with specific values.
Update the Pages field of myBook.
Print the details of myBook using fmt.Println.
💡 Why This Matters
🌍 Real World
Structs are used in Go to model real-world objects like books, users, or products by grouping related information together.
💼 Career
Understanding structs is essential for Go programming jobs, especially when working with data models, APIs, or databases.
Progress0 / 4 steps
1
Define the Book struct
Define a struct named Book with these fields: Title of type string, Author of type string, and Pages of type int.
Go
Hint

Use the type keyword to define a struct. Each field has a name and a type.

2
Create a Book variable
Inside the main function, create a variable named myBook of type Book with these values: Title set to "Go Programming", Author set to "Alice", and Pages set to 300.
Go
Hint

Use struct literal syntax with field names to create myBook.

3
Update the Pages field
Update the Pages field of myBook to 350 inside the main function.
Go
Hint

Use dot notation to change the Pages field value.

4
Print the book details
Use fmt.Println to print the myBook variable inside the main function.
Go
Hint

Use fmt.Println(myBook) to print the struct values.