- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Golang Program to convert int type variables to long
In go language a long type variable is not a separate data type but it is an extension made from the integer data type to store even larger types of integers. The main difference between an int data type and long data type is that int data type is 32 bits whereas long data type is 64 bits.
Syntax
func typeOf (x interface{})
The typeOf() function is used to get the type of any variable. This function is present in reflect package and it takes the variable whose type is to be determined as an argument. The function then returns the type of the variable specified as the result.
func ParseInt(s string, base int, bitSize int) (i int64, err error)
ParseInt() function is present in strconv package and is used to get the integer value from the string representation of a number. This function accepts the string value as argument along with the base value and bitSize and returns the integer value along with the error. The error variable contains the error if any generated in the conversion process.
func Itoa(x int) string
The Itoa() function is present in strconv package and is used to get the string representation of an integer variable when the base is 10. The function accepts the variable whose string equivalent is to be obtained as argument and returns the string representation which we can store and print on the screen.
Algorithm
Step 1 − First, we need to import the fmt and reflect packages.
Step 2 − Then, start the main() function.
Step 3 − Inside the main initialize the variable of integer data type named x and store value to it. further print the variable on the screen using fmt.Println() function.
Step 4 − Further, print the type of this variable by using TypeOf() function present in reflect package.
Step 5 − Now, in order to convert this variable into long pass the variable as argument to the int64() function and store the result in a new variable of same data type.
Step 6 − The new variable thus obtained is of long type and print the data inside this variable on the screen along with its type using TypeOf() function.
Example 1
In this example, we will use the type casting approach to convert the integer data type variables to long. Type casting works by passing the variable as an argument to int64() function.
package main import ( "fmt" "reflect" ) func main() { var x int = 5 fmt.Println("The variable to be converted is:", x) fmt.Println("Type of x is:", reflect.TypeOf(x)) fmt.Println() var y int64 = int64(x) fmt.Println("The variable obtained after converting the above value to long is:", y) fmt.Println("Type of y is:", reflect.TypeOf(y)) }
Output
The variable to be converted is: 5 Type of x is: int The variable obtained after converting the above value to long is: 5 Type of y is: int64
Example 2
In this example we will use ParseInt() function present in strconv package in order to convert the integer type variables to long.
package main import ( "fmt" "reflect" "strconv" ) func main() { var x int = 51 fmt.Println("The variable to be converted is:", x) fmt.Println("Type of x is:", reflect.TypeOf(x)) fmt.Println() y, _ := strconv.ParseInt(strconv.Itoa(x), 10, 64) fmt.Println("The variable obtained after converting the above value to long is:", y) fmt.Println("Type of y is:", reflect.TypeOf(y)) }
Output
The variable to be converted is: 51 Type of x is: int The variable obtained after converting the above value to long is: 51 Type of y is: int64
Conclusion
We have successfully compiled and executed a go language program to convert an integer type variable to long. We have used two examples here. In the first example we are using the type casting approach while in the second one we are using internal go functions in order to obtain the result. The functions that we have used in the second example are Itoa() and ParseInt().
- Related Articles
- Golang Program to convert long type variables to int
- Haskell Program to convert int type variables to long
- C++ Program to Convert long Type Variables to int
- C++ Program to Convert int Type Variables to long
- Haskell Program to convert long type variables into int
- Golang Program to convert double type variables to int
- Golang Program to convert int type variables to String
- Golang Program to convert string type variables into int
- Golang Program to Convert Char Type Variables to Int Using Inbuilt Functions
- Haskell Program to convert int type variables to char
- Swift Program to Convert Char type variables to Int
- Swift program to convert double type variables to int
- Haskell Program to convert int type variables to String
- Haskell Program to convert int type variables to double
- Haskell Program to convert double type variables to int
