Golang Tutorials - Data types Basic Guide with examples

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

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 TypeSize in bytesRange of values
int81 Byte-128 to 127
int162 Bytes-32768 to 32767
int324 Bytes-2147483648 to 2147483647
int648 Bytes-329223372036854775808768 to 9223372036854775807

Unsigned Integer types

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

Inbuilt TypeSize in bytesRange of values
uint81 Byte0 to 255
uint162 Byte0 to 65535
uint324 Byte0 to 4294967295
uint648 Byte0 to 18446744073709551615

Other Numeric types There are other numeric types bytes etc.

Inbuilt TypeSize in bytesRange of values
byte1 Byte0 to 255same as uint8
rune2 Byte0 to 65535 same as int32
uint4 or 8 Bytebased on Environment
int4 or 8 Bytebased on Environment
uintptr4 or 8 Byteunsigned 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

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, Floats types 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 TypeSize in bytesDescription
float324 ByteIEEE-754 Floating numeric values
float648 ByteIEEE-754 Floating numeric values
complex648 ByteComplex numbers with float32 real and imaginary parts
complex1284or 8 ByteComplex 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 such as alpha letters and numers. 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 such \n. Backticks enclosed string can contain multiple lines

var str1 = "Cloud Hadoop Blog"

var str2 = `This is programming blog for
   full stack technolgies`