Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

golang

What is type int 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 all different data types, whether they be just numbers, words, or any other type. To restrict the type of data that can be 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. int has a platform-dependent size, as, on a 32-bit system, it holds a 32 bit signed integer, while on a 64-bit system, it holds a 64-bit signed integer. A variable declared with the int data type will only store signed numeric values; if you, for example, try to store an alphanumeric value in it, the program would return an error.

Different versions of int

The numeric data type of int has several versions, which include:

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

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

Examples

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

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

Here, another approach to declaring an int variable is shown. In this example, we do not explicitly mention anywhere that the variable is of type int, but because we directly initialize it with an integer value, num still gets stored as a variable of type int, that we later check by using Printf to print out num's data type.

package main
import "fmt"
func main() {
// declaring and initializing an int variable
var num = -70;
// %T represents the type of the variable num
fmt.Printf("Data type of %d is %T\n", num, num);
}

It is also possible to create const values of type int instead of variables. The only difference is that const values are just variables whose values can not be changed from what it was initialized to. Finally, we 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 int = 10
// %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