Learn Golang Tutorials - Data types Basic Guide with examples


In this blog post series, We are going to learn basic inbuilt predefined data types in golang tutorials with examples.

Learn Golang Tutorials - Data types Basic Guide

Go language data types

Go is a modern opensource programming language from Google.

It is a static type language which means type checking will be done at compile time. Variable can be declared a type or inferred from type inference.

datatypes in golang define the same type of data that can be used to allocates memory size for storing the particular value.

It has several built-in types similar to any programming language.

There are several data types that can be categories based on similar data types

  • Numeric types - This represents numeric numbers which can be divided into Integers, Floating and other types
  • String types - This represents a group of characters
  • Boolean types - It represents boolean values - true or false
  • Custom Data types - It Contains different types like pointers, Array, Struct, Union, Map, and Channel types

Integer data types

These are used to store the positive and negative numbers including zero. It has various types based on size and signed types.

Signed Integer types

signed integer allows the values positives and negatives including zero. it also has int data type which changes based on machine environment

Inbuilt Type

Size in bytes

Range of values

int8

1 Byte

-128 to 127

int16

2 Bytes

-32768 to 32767

int32

4 Bytes

-2147483648 to 2147483647

int64

8 Bytes

-9223372036854775808 to 9223372036854775807

Unsigned Integer types 

Unsigned integer allows the values positive values including zero. It does not allow negative values

Inbuilt Type

Size in bytes

Range of values

uint8

1 Byte

0 to 255

uint16

2 Bytes

0 to 65535

uint32

4 Bytes

0 to 4294967295

uint64

8 Bytes

0 to 18446744073709551615

Other Numeric types
There are other numeric types byte etc..

Inbuilt Type

Size in bytes

Range of values

byte

1 Byte

same as uint8

rune

2 Bytes

int32

uint

4 Bytes or 8 bytes

based on Environment

int

4 Bytes or 8 bytes/td>

Based on Environment

uintptr

4 Bytes or 8 bytes/td>

unsigned int to store pointer bits

Example

This example covers the following things

  • Type inference example
  • The octal number always prefixed with 0
  • Hexadecimal number prefixed with 0x

This has no char data type in java language. byte and rune are used to represent char types. a byte represents characters in ASCII values. rune represents in UNICODE character in the UTF-8 format

package main  
import "fmt"  
func main() {  
 var int16Variable int16 = 12  
 var intVariable = 85 // Type inference example  
 var uintVariable uint = 40  
 var hexVariable = 0xBC    
 var octalVariable = 013   
 fmt.Printf("%d, %d, %d, %#x, %#o\n", int16Variable, intVariable, uintVariable, hexVariable, octalVariable)  
 var byteVariable byte = 'C'  
 var runeVariable rune = 'a'  
 fmt.Printf("%c - %d and %c - %U\n", byteVariable, byteVariable, runeVariable, runeVariable)  
  
}  

The output of the above program is

12, 85, 40, 0xbc, 013  
C - 67 and a - U+0061  

Floating numeric types 

Like any programming language, These are used to represents numbers with decimal values(3.21) for example, when you declared float value like this, compiler infer the type as float64. The default type is float64

var floatVariable = 47895.587  

And also complex data type default type is complex128. type inference applied on complex number also complex128

var complexVariable = 1 + 6i 

Type

Size in bytes

Description

float32

4 Bytes

IEEE-754 Floating numeric values

float64

8 Bytes

IEEE-754 Floating numeric values

complex64

8 bytes

Complex numbers with float32 real and imaginary parts

complex128

4 Bytes or 8 bytes/td>

Complex numbers with float64 real and imaginary parts

Boolean Datatypes 

Go language has data type bool to store boolean values. It has predefined values true or false

  
var boolVariable = true  
var booleVariable1 bool = false  

String Datatype 

The string is a group of characters. Characters in golang treated as bytes. The string can be declared using double quotes or backticks. Double quotes enclosed strings cannot be multiple lines but contains newline characters \n. Backticks enclosed string can contain multiple lines

var str1 = "Cloud Hadoop Blog"  
  
var str2 = `This is programming blog for   
   full stack technolgies` 
Similar Posts