package main import ( "fmt" "reflect" ) type Person struct { Name string Age int } func main() { p := Person{ Name: "John", Age: 30, } v := reflect.ValueOf(&p).Elem() f := v.FieldByName("Age") f.SetBool(40) fmt.Println(p) }
package main import ( "fmt" "reflect" ) func main() { b := true v := reflect.ValueOf(&b).Elem() v.SetBool(false) fmt.Println(b) }This example uses reflect.Value.SetBool to set a boolean variable to false. The package library for the reflect package is "reflect".