Data types specify the type of data that a valid Go variable can hold. In Go language, the type is divided into four categories which are as follows:
- Basic type: Numbers, strings, and booleans come under this category.
- Aggregate type: Array and structs come under this category.
- Reference type: Pointers, slices, maps, functions, and channels come under this category.
- Interface type
Here, we will discuss Basic Data Types in the Go language. The Basic Data Types are further categorized into three subcategories which are:
Numbers
In Go language, numbers are divided into three sub-categories that are:
- Integers: In Go language, both signed and unsigned integers are available in four different sizes as shown in the below table. The signed int is represented by int and the unsigned integer is represented by uint.
- Possible arithmetic operations : Addition, subtraction, multiplication, division, remainder
Data Type
|
Description
|
int8 |
8-bit signed integer |
int16 |
16-bit signed integer |
int32 |
32-bit signed integer |
int64 |
64-bit signed integer |
uint8 |
8-bit unsigned integer |
uint16 |
16-bit unsigned integer |
uint32 |
32-bit unsigned integer |
uint64 |
64-bit unsigned integer |
int |
Both int and uint contain same size, either 32 or 64 bit. |
uint |
Both int and uint contain same size, either 32 or 64 bit. |
rune |
It is a synonym of int32 and also represent Unicode code points. |
byte |
It is a synonym of uint8. |
uintptr |
It is an unsigned integer type. Its width is not defined, but its can hold all the bits of a pointer value. |
Example:
Go
package main
import "fmt"
func main() {
var X uint8 = 225
fmt.Println(X, X- 3 )
var Y int16 = 32767
fmt.Println(Y+ 2 , Y- 2 )
}
|
Output:
225 222
-32767 32765
Example of arithmetic operations :
Go
package main
import "fmt"
func main() {
var x int16 = 170
var y int16 = 83
fmt.Printf( " addition : %d + %d = %d\n " , x, y, x+y)
fmt.Printf( "subtraction : %d - %d = %d\n" , x, y, x-y)
fmt.Printf( " multiplication : %d * %d = %d\n" , x, y, x*y)
fmt.Printf( " division : %d / %d = %d\n" , x, y, x/y)
fmt.Printf( " remainder : %d %% %d = %d\n" , x, y, x%y)
}
|
Output:
addition : 170 + 83 = 253
subtraction : 170 - 83 = 87
multiplication : 170 * 83 = 14110
division : 170 / 83 = 2
remainder : 170 % 83 = 4
- Floating-Point Numbers: In Go language, floating-point numbers are divided into two categories as shown in the below table.
- Possible arithmetic operations : Addition, subtraction, multiplication, division.
- Three literal styles are available :
- decimal (3.15)
- exponential ( 12e18 or 3E10)
- mixed (13.16e12)
Data Type |
Description
|
float32 |
32-bit IEEE 754 floating-point number |
float64 |
64-bit IEEE 754 floating-point number |
Example:
Go
package main
import "fmt"
func main() {
a := 20.45
b := 34.89
c := b-a
fmt.Printf( "Result is: %f" , c)
fmt.Printf( "\nThe type of c is : %T" , c)
}
|
Output:
Result is: 14.440000
The type of c is : float64
Example of arithmetic operations for floating point numbers :
Go
package main
import "fmt"
func main() {
var x float32 = 5.00
var y float32 = 2.25
fmt.Printf( "addition : %g + %g = %g\n " , x, y, x+y)
fmt.Printf( "subtraction : %g - %g = %g\n" , x, y, x-y)
fmt.Printf( "multiplication : %g * %g = %g\n" , x, y, x*y)
fmt.Printf( "division : %g / %g = %g\n" , x, y, x/y)
}
|
Output:
addition : 5 + 2.25 = 7.25
subtraction : 5 - 2.25 = 2.75
multiplication : 5 * 2.25 = 11.25
division : 5 / 2.25 = 2.2222223
- Complex Numbers: The complex numbers are divided into two parts are shown in the below table. float32 and float64 are also part of these complex numbers. The in-built function creates a complex number from its imaginary and real part and in-built imaginary and real function extract those parts.
- There are few built-in functions in complex numbers:
- complex – make complex numbers from two floats.
- real() – get real part of the input complex number as a float number.
- imag() – get imaginary of the input complex number part as float number
Data Type |
Description
|
complex64 |
Complex numbers which contain float32 as a real and imaginary component. |
complex128 |
Complex numbers which contain float64 as a real and imaginary component. |
Example:
Go
package main
import "fmt"
func main() {
var a complex128 = complex( 6 , 2 )
var b complex64 = complex( 9 , 2 )
fmt.Println(a)
fmt.Println(b)
fmt.Printf( "The type of a is %T and " +
"the type of b is %T" , a, b)
}
|
Output:
(6+2i)
(9+2i)
The type of a is complex128 and the type of b is complex64
Built-in functions example :
Go
package main
import "fmt"
func main() {
comp1 := complex( 10 , 11 )
comp2 := 13 + 33i
fmt.Println( "Complex number 1 is :" , comp1)
fmt.Println( "Complex number 1 is :" , comp2)
realNum := real(comp1)
fmt.Println( "Real part of complex number 1:" , realNum)
imaginary := imag(comp2)
fmt.Println( "Imaginary part of complex number 2:" , imaginary)
}
|
Output:
Complex number 1 is : (10+11i)
Complex number 1 is : (13+33i)
Real part of complex number 1: 10
Imaginary part of complex number 2: 33
Booleans
The boolean data type represents only one bit of information either true or false. The values of type boolean are not converted implicitly or explicitly to any other type.
Example:
Go
package main
import "fmt"
func main() {
str1 := "GeeksforGeeks"
str2:= "geeksForgeeks"
str3:= "GeeksforGeeks"
result1:= str1 == str2
result2:= str1 == str3
fmt.Println( result1)
fmt.Println( result2)
fmt.Printf( "The type of result1 is %T and " +
"the type of result2 is %T" ,
result1, result2)
}
|
Output:
false
true
The type of result1 is bool and the type of result2 is bool
Strings
The string data type represents a sequence of Unicode code points. Or in other words, we can say a string is a sequence of immutable bytes, means once a string is created you cannot change that string. A string may contain arbitrary data, including bytes with zero value in the human-readable form. Strings can be concatenated using plus(+) operator.
Example:
Go
package main
import "fmt"
func main() {
str := "GeeksforGeeks"
fmt.Printf( "Length of the string is:%d" ,
len(str))
fmt.Printf( "\nString is: %s" , str)
fmt.Printf( "\nType of str is: %T" , str)
}
|
Output:
Length of the string is:13
String is: GeeksforGeeks
Type of str is: string
String concatenation example:
Go
package main
import "fmt"
func main() {
var str1 string = "STRING_"
var str2 string = "Concatenation"
fmt.Println( "New string : " , str1+str2)
}
|
Output:
New string : STRING_Concatenation
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
23 Mar, 2023
Like Article
Save Article