Challenge: Creating a Structure
Swipe to show menu
Task
Create a structure named Car with fields company, owner, miles, and year in the same order. Assign appropriate data types to them based on the provided code.
index.go
12345678910111213141516package main import "fmt" // Write code below this line // Write code above this line func main() { var car1 Car car1.company = "Tesla" car1.owner = "Tegmark" car1.miles = 10.7 car1.year = 2021 fmt.Println(car1) }
Hint
Create a structure called Car with the fields company, owner, miles, and year in the same order. Assign the appropriate data types to each field based on the given code.
Solution
package main
import "fmt"
// Write code below this line
type Car struct {
company string
owner string
miles float32
year int
}
// Write code above this line
func main() {
var car1 Car
car1.company = "Tesla"
car1.owner = "Tegmark"
car1.miles = 10.7
car1.year = 2021
fmt.Println(car1)
}
Everything was clear?
Thanks for your feedback!
Section 6. Chapter 8
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Section 6. Chapter 8