reflect.Set() Function in Golang with Examples Last Updated : 10 May, 2020 Comments Improve Suggest changes Like Article Like Report Go language provides inbuilt support implementation of run-time reflection and allowing a program to manipulate objects with arbitrary types with the help of reflect package. The reflect.Set() Function in Golang is used to assigns x to the value v. To access this function, one needs to imports the reflect package in the program. Syntax: func (v Value) Set(x Value) Parameters: This function accept one parameter of Value type (x). Return Value: This function does not returns any value. Below examples illustrate the use of the above method in Golang: Example 1: C // Golang program to illustrate // reflect.Set() Function package main import ( "fmt" "reflect" "unsafe" ) // Main function func main() { var s = struct{ foo int }{654} var i int rs := reflect.ValueOf(&s).Elem() rf := rs.Field(0) ri := reflect.ValueOf(&i).Elem() rf = reflect.NewAt(rf.Type(), unsafe.Pointer(rf.UnsafeAddr())).Elem() ri.Set(rf) rf.Set(ri) fmt.Println(rf) fmt.Println(ri) } Output: 654 654 Example 2: C // Golang program to illustrate // reflect.Set() Function package main import ( "fmt" "reflect" ) func sum(args []reflect.Value) []reflect.Value { a, b := args[0], args[1] if a.Kind() != b.Kind() { fmt.Println("??? ????.") return nil } switch a.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: return []reflect.Value{reflect.ValueOf(a.Int() + b.Int())} case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: return []reflect.Value{reflect.ValueOf(a.Uint() + b.Uint())} case reflect.Float32, reflect.Float64: return []reflect.Value{reflect.ValueOf(a.Float() + b.Float())} case reflect.String: return []reflect.Value{reflect.ValueOf(a.String() + b.String())} default: return []reflect.Value{} } } func makeSum(fptr interface{}) { fn := reflect.ValueOf(fptr).Elem() v := reflect.MakeFunc(fn.Type(), sum) fn.Set(v) } // Main function func main() { var stringSum func(string, string) string makeSum(&stringSum) fmt.Println(stringSum("Value_1 : ", "Geeks_1")) } Output: Value_1 : Geeks_1 Comment More infoAdvertise with us Next Article reflect.Set() Function in Golang with Examples S SHUBHAMSINGH10 Follow Improve Article Tags : Go Language Golang-reflect Similar Reads reflect.Select() Function in Golang with Examples Go language provides inbuilt support implementation of run-time reflection and allowing a program to manipulate objects with arbitrary types with the help of reflect package. The reflect.Select() Function in Golang is used to executes a select operation described by the list of cases. To access this 2 min read reflect.SetBool() Function in Golang with Examples Go language provides inbuilt support implementation of run-time reflection and allowing a program to manipulate objects with arbitrary types with the help of reflect package. The reflect.SetBool() Function in Golang is used to sets v's underlying value. To access this function, one needs to imports 2 min read reflect.String() Function in Golang with Examples Go language provides inbuilt support implementation of run-time reflection and allowing a program to manipulate objects with arbitrary types with the help of reflect package. The reflect.String() Function in Golang is used to get the string v's underlying value, as a string. To access this function, 2 min read reflect.Type() Function in Golang with Examples Go language provides inbuilt support implementation of run-time reflection and allowing a program to manipulate objects with arbitrary types with the help of reflect package. The reflect.Type() Function in Golang is used to get v's type. To access this function, one needs to imports the reflect pack 1 min read reflect.Uint() Function in Golang with Examples Go language provides inbuilt support implementation of run-time reflection and allowing a program to manipulate objects with arbitrary types with the help of reflect package. The reflect.Uint() Function in Golang is used to get v's underlying value, as a uint64. To access this function, one needs to 2 min read Like