SoFunction
Updated on 2025-03-05

Example code for golang string to 64-bit integer

Golang string to 64-bit integer

In Go, you can use the ParseInt function in the strconv package to convert a string to a 64-bit integer. Here is a sample code:

package main  
import (  
 "fmt"  
 "strconv"  
)  
func main() {  
 str := "12345"  
 num, err := (str, 10, 64)  
 if err != nil {  
 ("Conversion failed:", err)  
 return  
 }  
 ("Conversion result:", num)  
}

In the above code, we first import the fmt and strconv packages. Then, we define a string variable str with a value of "12345". Next, we use a function to convert the string to a 64-bit integer and store the result in the variable num. The first parameter of the function is the string to be converted, the second parameter is the cardinality of the integer represented by the string (decimal is used here), and the third parameter is the converted integer type (the 64-bit integer is used here). If the conversion fails, the err variable will contain error information. Finally, we use the function to print the conversion result.

Running the above code will output:

Conversion result: 12345

Note that if the string cannot be converted to a 64-bit integer, the function returns an error. So, in real applications, you should check for errors to ensure that the conversion is successful.

golang basics - strings and int and int64

1. Code

package main
import (
	"fmt"
	"reflect"
	"strconv"
)
func main() {
	var testStr string = "123456"
	// String to int	intNum, _ := (testStr)
	("intNum =", intNum, ", intNum Type is", (intNum))
	// String to int64	int64Num, _ := (testStr, 10, 64)
	("int64Num=", int64Num, ", int64Num Type is", (int64Num))
	// ===================================================
	// int to string	var intNum2 int = 123456
	var str1 string = (intNum2)
	("str1 =", str1, ", str1 Type is", (str1))
	// int64 to string	var int64Num2 int64 = 123456
	var str2 string = (int64Num2, 10)
	("str2 =", str2, ", str2 Type is", (str2))
	("------------------------------------------------------------------------------------------------------------------------------)
	var num3 int = 30
	var num4 int64
	num4 = int64(num3)
	("num3 的Type is %T ,num3= %v \n", num3, num3)
	("num4 的Type is %T ,num4= %v \n", num4, num4)
	var num5 int64 = 20
	var num6 int
	num6 = int(num5)
	("num5 的Type is %T ,num5= %v \n", num5, num5)
	("num6 的Type is %T ,num6= %v \n", num6, num6)
}

Running results:

intNum = 123456 , intNum type is int
int64Num= 123456 , int64Num type is int64
str1 = 123456 , str1 type is string
str2 = 123456 , str2 type is string
--------------------------------------------------------------------------------------------------------------------------------
num3 type is int ,num3= 30
num4 type is int64, num4= 30
num5 type is int64, num5= 20
The type of num6 is int, num6= 20

2. Summary

2.1. String and int are transferred

// String to intintNum, _ := (testStr)
// int to stringvar str1 string = (intNum2)

2.2. String and int64 are transferred

// String to int64// Parameter 1: numeric type string// Parameter 2: Category of numeric strings, such as binary, octal, decimal, hexadecimal// Parameter 3: bitSize, which means the limit of the numerical range when string is converted to int64. For example, int8 int16 int32 int64 The value range of different numeric types is different.int64Num, _ := (testStr, 10, 64)
// int64 to string// Parameter 1: Int64 type number. If it is int, int8 int16 int32, you need to use int64 (number) conversion// Parameter 2: A string indicating what kind of binary number to convert int64, such as binary, octal, decimal, and hexadecimal.  The value must be between 2 and 36.var str2 string = (int64Num2, 10) 

Example:Stringe It belongs to hexadecimal and is 14 in decimal.

var testStr3 string = "e"
int64Num, _ := (testStr3, 16, 8) 
("int64Num=", int64Num, ", int64Num Type is", (int64Num))

Running results:

int64Num= 14 , int64Num type is int64

2.3. The easiest way to transfer int and int64

var intNum int = int(int64digits)
var int64Num int64 = int64(intnumber)

This is the end of this article about golang string to 64-bit integer. For more related golang string to integer content, please search for my previous article or continue browsing the related articles below. I hope everyone will support me in the future!