type Car struct { Make string Model string Year int NumDoors int } // create a new instance of Car myCar := Car{Make: "Honda", Model: "Civic", Year: 2018, NumDoors: 4} // get the value of the Year field using reflect yearField := reflect.ValueOf(myCar).FieldByName("Year") yearValue := yearField.Int() // print the value of the Year field fmt.Println("Year: ", yearValue) // output: Year: 2018
type Person struct { FirstName string LastName string Age int } // create a new instance of Person person := Person{FirstName: "John", LastName: "Doe", Age: 30} // update the Age field using reflect ageField := reflect.ValueOf(&person).Elem().FieldByName("Age") ageField.SetInt(35) // print the updated value of the Age field fmt.Println("Updated age: ", person.Age) // output: Updated age: 35In this example, we create a new instance of the `Person` struct and update the value of the `Age` field using the `reflect.ValueOf` method, `Elem` method, and `FieldByName` method from the Go reflect package. We then print out the updated value of the `Age` field to the console. Package Library: reflect.