Golang Tutorials - Data types Basic Guide with examples

Learn basic inbuilt predefined data types in golang tutorials with examples.

golang Data types Basic Guide

Go language data types

Go is a modern open-source programming language from Google.

It is a statically typed language which means type checking is 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 allocate memory size for storing the particular value.

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

Several data types can be categorized 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

Golang 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 an int data type that changes based on the 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|-329223372036854775808768 to 9223372036854775807|

Unsigned Integer types

An 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 Byte 0 to 65535
uint32 4 Byte 0 to 4294967295
uint64 8 Byte 0 to 18446744073709551615

Other Numeric types There are other numeric types bytes etc.

Inbuilt Type Size in bytes Range of values
byte 1 Byte 0 to 255same as uint8
rune 2 Byte 0 to 65535 same as int32
uint 4 or 8 Byte based on Environment
int 4 or 8 Byte based on Environment
uintptr 4 or 8 Byte unsigned int to store pointer bits


This example covers the following things.

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

It has no char data type in java language. byte and rune are used to represent char types. a byte represents characters in ASCII values. the 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 represent numbers with decimal values(3.21) for example, when you declared a float value like this, the compiler infers 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 the complex number also complex128

var complexVariable = 1 + 6i 
Inbuilt Type Size in bytes Description
float32 4 Byte IEEE-754 Floating numeric values
float64 8 Byte IEEE-754 Floating numeric values
complex64 8 Byte Complex numbers with float32 real and imaginary parts
complex128 4or 8 Byte Complex numbers with float64 real and imaginary parts
uintptr 4 or 8 Byte unsigned int to store pointer bits

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 are 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` 
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
You'll get a notification every time a post gets published here.