The Go Language – basic data types – Part 3

Hi All,
Greetings for the day!!!
We are continuing series on Go lang. In following two previous articles we discussed about – introduction to Go and wrote our first simple Go program
In this article we will move ahead and will discuss basic data types in Go
Take away from this article
- Understand the basic data types in Go – Integers, Floating-point numbers, Booleans
- Exploring new package – math
Following are the basic data types Go supports
- Integers
- Floating-point numbers
- Complex numbers
- Booleans
- Strings
In this article we will discuss – Integers, Floating-point numbers and Booleans.
For Strings and Complex numbers we will have separate articles
Integers
- Go supports both – signed and unsigned integers
- Following integer types are supported
uint8 |
uint16 |
uint32 |
uint64 |
int8 |
int16 |
int32 |
int64 |
- Actually we mostly just use int and uint
- Size of int and uint mostly depends on compiler based on the hardware
- Examples – declaring variables of type integers
- var intCounter int
- var intCounter, intResult int
- var intCounter = 10
- intCoutnter :=10
- intCounter, intResult := 10,10
- If any value is not assigned to integer variables then default value will be ZERO (0)
- Regarding reminder (%) operator for integers:
- In Go reminder operator is only applied for integers
- Also, sign of reminder operator is always same as sign of dividend – -11/2 = -1 and also -11/-2 = -1
package main
//importing required packages
import (
"fmt"
)
//main function
func main() {
//declaring variables
var dividend = -11
var dividor = 2
var reminderVal = dividend % dividor
fmt.Println("Respective reminder value is", reminderVal)
}
Float Types
- Following float types are supported
float32 |
float64 |
complex64 |
complex128 |
- Finding maximum limit of float types – we can use “math” package to know the maximum limit of float32 and float64 types – below is the simple program
package main
import (
"fmt"
"math"
)
func main() {
//Getting max limit of float32 type
var maxfloat32limit = math.MaxFloat32
//Getting max limit of float64 type
var maxfloat64limit = math.MaxFloat64
fmt.Println("Max limit of Float32 is ", maxfloat32limit)
fmt.Println("Max limit of Float64 is ", maxfloat64limit)
}

- Examples:
- var float32Var float32 = 2.71
- var float64Var float64
Boolean Types
- Two possible values only – True or False
- Conditions in if and for statement are boolean
- Examples:
- var boolVar bool
- var boolVar = true
- boolVar := false
Thanks for reading !!! Feel free to discuss in case any issue / suggestion / thoughts !!!
HAVE A GREAT TIME AHEAD !!! LIFE IS BEAUTIFUL 🙂
2 Responses
[…] The Go Language – basic data types – Part 3 […]
[…] The Go Language – basic data types – Part 3 […]
You must log in to post a comment.