0
0
Goprogramming~15 mins

Defining structs in Go - Mini Project: Build & Apply

Choose your learning style9 modes available
Defining structs
📖 Scenario: You are creating a simple program to store information about books in a library.
🎯 Goal: You will define a struct to represent a book with its title and author, then create an instance of this struct and display its details.
📋 What You'll Learn
Define a struct named Book with two fields: Title and Author, both of type string.
Create a variable named myBook of type Book with the title "The Go Programming Language" and author "Alan A. A. Donovan".
Print the book's title and author in the format: Title: The Go Programming Language, Author: Alan A. A. Donovan.
💡 Why This Matters
🌍 Real World
Structs are used to group related data together, like storing information about books, people, or products in real applications.
💼 Career
Understanding structs is essential for Go programming jobs, as they are the foundation for creating complex data models and working with APIs.
Progress0 / 4 steps
1
Define the Book struct
Define a struct named Book with two fields: Title and Author, both of type string.
Go
Hint

Use type Book struct {} and inside add Title string and Author string.

2
Create a myBook variable of type Book
Inside the main function, create a variable named myBook of type Book with Title set to "The Go Programming Language" and Author set to "Alan A. A. Donovan".
Go
Hint

Use myBook := Book{Title: "The Go Programming Language", Author: "Alan A. A. Donovan"} inside main.

3
Print the book's title and author
Add a fmt.Println statement inside main to print the book's title and author in this exact format: Title: The Go Programming Language, Author: Alan A. A. Donovan. Use myBook.Title and myBook.Author.
Go
Hint

Use fmt.Println("Title:", myBook.Title+",", "Author:", myBook.Author) to print the details.

4
Run the program to see the output
Run the program to display the book's title and author exactly as: Title: The Go Programming Language, Author: Alan A. A. Donovan.
Go
Hint

Make sure the output matches exactly with the title and author separated by a comma.