Updated May 23, 2023
Introduction to Golang Reflect
The Golang Reflect allows us to change or modify the object or any variable at the dynamic. In more technical words, it allows us to manipulate objects’ values dynamically. We need to use the package reflection of the go language to use this feature. We have a function in reflecting called ValueOf(); this function allows us to get a new change value, remember the value will be stored inside an interface (Here, interface means if we want to access the function, then we need to import its function), we can use the function as reflect.ValueOf().
Syntax
Below is a simple syntax for the reflection in the go language, and we can explain the below syntax in the following way with each parameter:
func reflect.ValueOf(interface interface{}) Value
Explanation:
We need to import the function as we store it inside an interface, and according to the rules for any interface, we need to use all its parts of it, and we can define the property of that function according to our requirements. Second, we have to pass the interface here; we can use objects like {12,4,56}, etc., which can be passed to the ValueOf function as the arguments. Once we have done with the passing of the value, we can return a new value in the form of the matter.
Remember, as we have discussed, that it allows us to change the value dynamically; as we know, the go language is statically typed, which means we need to define the variable data type and inform the compiler about the size and type of the variable to use further and create equivalent space such kind of the variables.
How does Reflect work in the Go language?
Before discussing the reflection in the go language, we must focus on its introduction and critical uses. You know that go language is a statically typed language; statically typed means we need to mention the variable data type like integer or string or float, in the case of other programming languages like Nodejs or Javascript, and in the case of Ruby, we do not require to inform the compiler about the data type of the variable, but what we will do in case if we want to do some dynamic change in the object, the main benefit of doing dynamic changes we avoid compilation because when we define the variable data type which means we are setting and defying the spaces for the variable, but in case of dynamic language we do not require to inform the compiler about the data types of the variable which means there will be too much manipulation at the time of running because data type and need to check and allocation of memory will also be done at that time only. We can focus on some important points which revealed the working of the reflection.
Reflect is a package in the go language that contains the function ValueOf. This function allows us to manipulate the object dynamically. We can append one array of things to another dynamically, which makes it flexible to handle dynamic conditions. Some essential functions like ValueOF and Append belong to the reflection in the go language.
Examples to Implement Golang Reflect
We will learn more about its working with the help of the examples:
Below we have given some examples related to the reflection; in the below examples, we are performing the appending and manipulations of the string of array. We are also manipulating the integer of the array. We are printing the manipulated output of the array of strings and an array of integers. In case we want to see the output of the below examples, then we can create a file with any name like creating a file reflect.go and copy and paste the below examples on the files and run the command go run reflect.go, and we can see the outputs.
Example #1
Below is a simple example using the reflect Append and ValueOF functions on the integer of the array; here, we are combining two arrays of data; please see the example below, along with the output screen.
Code:
package main
import (
"fmt"
"reflect"
)
func main() {
x := []int{4, 7}
var y reflect.Value = reflect.ValueOf(&x)
y = y.Elem()
fmt.Println("The Slice is:", x)
y = reflect.Append(y, reflect.ValueOf(78))
fmt.Println("The Slice value after data appending is:", y)
}
Output:
Example #2
Below is a simple example with the use of the reflect Append and ValueOF functions on the string of the array; please see the below example along with the screen of the output.
Code:
package main
import (
"fmt"
"reflect"
)
func main() {
x := []string{"ranjan", "kumar"}
var y reflect.Value = reflect.ValueOf(&x)
y = y.Elem()
fmt.Println("The Slice is:", x)
y = reflect.Append(y, reflect.ValueOf("Pandey"))
fmt.Println("The Slice value after data appending is:", y)
}
Output:
Example #3
Below is a simple example with the use of the reflect Append and ValueOF functions on the integer of the array; please see the below example along with the screen of the output.
Code:
package main
import (
"fmt"
"reflect"
)
func main() {
source := reflect.ValueOf([]int{11, 12, 19})
destination := reflect.ValueOf([]int{13, 21, 31})
fmt.Println("The value after using reflect is",source, destination)
}
Output:
Example #4
Please see the below example along with the screen of the output.
Code:
package main
import (
"fmt"
"reflect"
)
func main() {
fullname := []string{"ranjan", "kumar","Pandey"}
var address reflect.Value = reflect.ValueOf(&fullname)
address = address.Elem()
fmt.Println("The Slice is:", fullname)
address = reflect.Append(address, reflect.ValueOf("Chennai"))
fmt.Println("The Slice value after data appending is:", address)
}
Output:
Conclusion
From the above example, we have learned about the basic concept of reflection in the go language, and we also learned the syntax of the reflect. We learned the working and importance of reflection in the go language. We saw some important examples for more clarity.
Recommended Articles
We hope that this EDUCBA information on “Golang Reflect” was beneficial to you. You can view EDUCBA’s recommended articles for more information.