Go Datatypes
Go Datatypes
We use data types in Golang to determine the type of data associated with
variables. For example,
Here, int is a data type that specifies that the age variable can store integer
data.
The basic data types in Golang are
Data
Description Examples
Types
var id int
signed integer int - can hold both positive and negative integers
unsigned integer uint - can only hold positive integers
There are different variations of integers in Go programming.
Note: Unless we have a specific requirement, we usually use the int keyword
to create integers.
Example 1: Understanding Integer Type
package main
import "fmt"
func main() {
var integer1 int
var integer2 int
integer1 = 5
integer2 = 10
fmt.Println(integer1)
fmt.Print(integer1)
}
Output
5
10
34.2
Note: If we define float variables without specifying size explicitly, the size of
the variable will be 64 bits. For example,
package main
import "fmt"
func main() {
var salary1 float32
var salary2 float64
salary1 = 50000.503882901
fmt.Println(salary1)
fmt.Println(salary2)
}
Output
50000.504
50000.503882901
Keyword: string
Here's an example,
package main
import "fmt"
func main() {
var message string
message = "Welcome to Programiz"
fmt.Println(message)
Output
Welcome to Programiz
package main
import "fmt"
func main() {
var boolValue bool
boolValue = false
fmt.Println(boolValue)
}
Output
false