
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert Primitive Types to Objects in Go
In this Golang program, converting primitive data types (such as int, char, float, etc) to objects is known as "Boxing". It is a procedure that transforms a primitive type into the matching object type, such as an integer into a float, a character into a char, etc. This enables the programmer to use the fields and methods of the relevant object type and treat primitive data types as objects. We will use two methods to execute this program.
Method 1: Using Reflect
In this method, the primitive types are transformed into objects using the reflect package. The type and data of the input argument are returned in a value object by the reflect.ValueOf function. The returned value object's type information is returned by the Type method.
Syntax
reflect.ValueOf()
The reflect.ValueOf function in Go returns a reflect.Value that represents the run-time value passed as its argument. The reflect. Value type provides a way to inspect the properties of an arbitrary Go value, such as its type, kind, and value. The returned reflect.Value can be used to extract information about the underlying value and to manipulate it dynamically, although doing so can be slow and is usually considered a last resort.
Algorithm
Step 1 ? Create a package main and declare fmt(format package) and reflect package in the program where main produces executable codes and fmt helps in formatting input and output.
Step 2 ? Specify the primitive data types that you wish to convert, such as the integer val, the string strval, and the boolean boolval.
Step 3 ? Each primitive type should be converted to an object using the reflect.ValueOf function. An interface is sent to the reflect.ValueOf function, which returns a reflect. In Go, a value is represented by a value object.
Step 4 ? To learn more about the type of the reflect.Value object, call the Type method on it.
Step 5 ? Print the type information on the console using fmt.Println() function where ln means new line.
Step 6 ? For each primitive type you want to convert, repeat steps 4 and 5.
Step 7 ? Each transformed primitive type's type information will be printed by the program.
Example
In this example we will use reflect.ValueOf function to convert primitive types to objects. Let's see through the code.
package main import ( "fmt" "reflect" ) func main() { val := 42 strval := "alexa" boolval := true numvalue := reflect.ValueOf(val) //convert the primitive types to objects using this built-in function strvalue := reflect.ValueOf(strval) boolvalue := reflect.ValueOf(boolval) fmt.Println("The conversion of primitive types to objects can be represented as:") fmt.Println("val type:", numvalue.Type()) //print the types fmt.Println("str type:", strvalue.Type()) fmt.Println("bool type:", boolvalue.Type()) }
Output
The conversion of primitive types to objects can be represented as: val type: int str type: string bool type: bool
Method 2: Using Type-casting
Using type casting, we transform each primitive type in this example into an object of type Object by defining an empty interface called Object. Each object's type is printed using the fmt.Printf function, with the format verb %T used to output the type.
Algorithm
Step 1 ? Create a package main and declare fmt(format package) package in the program where main produces executable codes and fmt helps in formatting input and output.
Step 2 ? To represent an object, define an empty interface object.
Step 3 ? Specify the primitive data types that you wish to convert, such as the integer val, the string strval, and the boolean boolval.
Step 4 ? To create an object of type Object from each primitive type, use type casting. The type casting syntax is Object(value), where Object denotes the target type and value denotes the target value.
Step 5 ? Each object's type should be printed using the fmt.Printf function and the format verb %T.
Step 6 ? For each primitive type you want to convert, repeat steps 4 and 5.
Step 7 ? Each transformed primitive type's type information will be output by the application.
Step 8 ? fmt.Println() function is also used to print statements in this program, here ln means new line.
Example
In this example we will use typecasting to convert primitive types to objects. Let's see through the code ?
package main import "fmt" type Object interface{} func main() { val := 42 strval := "hello" boolval := true valObj := Object(val) //convert the primitive types to objects strObj := Object(strval) boolObj := Object(boolval) fmt.Println("The conversion of primitive types to objects can be represented as:") fmt.Printf("val type: %T\n", valObj) //print the types fmt.Printf("strval type: %T\n", strObj) fmt.Printf("boolval type: %T\n", boolObj) }
Output
The conversion of primitive types to objects can be represented as: val type: int strval type: string boolval type: bool
Conclusion
We executed the program of converting primitive types to objects using two examples. In the first example we used reflect package and in the second example we used typecasting.