Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

golang

What is type int64 in Golang?

Faraz Karim

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

In the Go programming language, variables are containers marked by identifiers or names, which can hold different values in a program. These variables can hold many different data types, whether they are numbers, words, or any other type. To restrict the type of data stored inside these variables, we need to specify the data type of the variables.

int is one of the available numeric data types in Go used to store signed integers. int64 is a version of int that only stores signed numeric values composed of up to 64 bits. So, for example, if you try to store a string value in it, or an integer beyond what can be made using 64 bits, the program would return an error or result in an integer overflow.

Similar variants

The numeric data type of int has several other versions in addition to int64, which include:

  • int8
  • int16
  • int32
  • uint8
  • uint16
  • uint32
  • uint64

The data types starting from int store signed integers, while those starting with uint contain unsigned integers. The numeric value that follows each data type represents the number of bytes stored.

Range

A variable of type int64 can store integers ranging from -9223372036854775808 til **9223372036854775807.

If an int64 variable is assigned a value beyond the range mentioned above, then an overflow error occurs, which basically means that the int64 variable cannot properly store the assigned number.

Overflow

The following code shows the positive limit of the values that an int64 variable can store and what happens if you try to store something larger than the stipulated range of values:

package main
import "fmt"
func main() {
// initializing with the maximum allowed positive number
var num int64 = 9223372036854775807
// printing the value and data type
fmt.Printf("Value is: %d and type is: %T\n", num, num);
// making the value out of range by incrementing by 1
num = num+1
// printing out new value and type
fmt.Printf("Value is: %d and type is: %T\n", num, num);
}

As we can see from the outputs of the code above, when we stored the value 9223372036854775807 in num and printed it, the value came out to be 2147483647. However, just incrementing it by 1 pushed it out of the allowed range, which resulted in the stored value being interpreted as -9223372036854775808 when printed again. When this happens, we call it an overflow.

Examples

In the following example, we declare a variable num and explicitly state its data type to be int64. Later, we can use the Printf function to see that num is indeed stored as an int64 data type:

package main
import "fmt"
func main() {
var num int64
num = 200
fmt.Printf("Data type of %d is %T\n", num, num);
}

It is also possible to create const values of type int64 instead of variables. The only difference is that const values are just variables whose values cannot be changed from what they were initialized to. We then check the stored data type again by printing it out using the Printf function.

const values must be declared and initialized in the same line.

package main
import "fmt"
func main() {
// declaring and initializing a const value with an integer
const c_num int64 = 200
// %T represents the type of the variable num
fmt.Printf("Data type of %d is %T\n", c_num, c_num);
}

RELATED TAGS

golang

CONTRIBUTOR

Faraz Karim
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring