The Go Language – basic data types – Part 3

Exploring Go basic types
Exploring Go basic types

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
Table 1 : Starting with Go – Basic data types – Integers

  • 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)
}

Exploring Go basic types - Integer types - demonstration of reminder operator
fig : Exploring Go basic types – Integer types – demonstration of reminder operator

Float Types

  • Following float types are supported

float32
float64
complex64
complex128
Table 2 : Starting with Go – Basic data types – Float

  • 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)
}

Exploring Go basic types - Float types - program to know the max limit of Float32 and Float64 types
fig : Exploring Go basic types – Float types – program to know the max limit of Float32 and Float64 types

  • 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 🙂

Prasham Sabadra

LIFE IS VERY BEAUTIFUL :) ENJOY THE WHOLE JOURNEY :) Founder of Knowledge Junction and live-beautiful-life.com, Author, Learner, Passionate Techie, avid reader. Certified Professional Workshop Facilitator / Public Speaker. Scrum Foundation Professional certificated. Motivational, Behavioral , Technical speaker. Speaks in various events including SharePoint Saturdays, Boot camps, Collages / Schools, local chapter. Can reach me for Microsoft 365, Azure, DevOps, SharePoint, Teams, Power Platform, JavaScript.

You may also like...

2 Responses

  1. May 22, 2022

    […] The Go Language – basic data types – Part 3 […]

  2. May 29, 2022

    […] The Go Language – basic data types – Part 3 […]

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: