SoFunction
Updated on 2025-03-05

The basic strings of golang are converted to int and int64 types

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:

StringeIt 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)

Summarize

This is the article about the conversion of golang basic strings and int and int64 types. This is the end of this article. For more related contents of golang strings and int, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!