Skip to main content

Command Palette

Search for a command to run...

Primitive Data Types in Go

Updated
3 min read

Unlike languages like Python and Javascript, Go is statically typed. That means once a variable type is defined, the variable will only store that type of data.

Go has 3 basic types

  1. bool

  2. Numeric

  3. string

Bool

To declare a variable and then initalize it, you can do it following way:

var myVar bool                     //type declaration
myVar = false                     //value initialization
var myVar2 bool = false //type declaration and initialization

There is a special short cut operator to declare and define variables in Go:

myVar := false

In the above code, using the := operator, we can directly declare and assign the variable a value. The type is automatically inferred from the value.

Integers

There are two types of integers

  1. Signed integers

  2. Unsigned integers

Signed integers are declared with the int prefixed keywords

var x int = 200
var y int8 = -100

There are five types of int:

  1. int -> 32 bits for 32 bit systems and 64 bits for 64 bit systems.

  2. int8 -> 8 bit integer storing from -128 to 127

  3. int16 -> 16 bit integer storing from -32768 to 32767

  4. int32 -> 32 bit integer storing from +/- 2^31

  5. int64 -> 64 bit integer storing from +/- 2^63

You can use these keywords to define integers.

Unsigned integers can be defined using uint prefixed key

var x uint = 1200
var x uint16 = 2456

Similar to int, you have five types of uint

  1. int -> 32 bits for 32 bit systems and 64 bits for 64 bit systems.

  2. int8 -> 8 bit storing positive values from 0 to 2^8

  3. int16 -> 16 bit storing positive values from 0 to 2^16

  4. int32 -> 32 bit storing positive values from 0 to 2^32

  5. int64 -> 64 bit storing positive values from 0 to 2^64

There are two aliases for uint8 and int32

  1. btye -> alias for uint8

  2. rune -> alias for int32

var m rune = -42
var n byte = 37
fmt.Println(reflect.TypeOf(m), reflect.TypeOf(n))

m will be int32 whereas n will be uint8

Float

There are two types of floats

  1. float32 -> 32 bit float storing from -3.4e+38 to 3.4e+38

  2. float64 -> 64 bit float storing from -1.7e+308 to 1.7e+308

The default type is float64 in case you use := for defining a variable.

x := 3.44                //float64
var y float32 = 3e+38
var y float64 = 17.323

String

Strings can be defined using the string keyword

var x string = "Hello, World!"
y := "My name is awesome!"

Complex

Go also supports complex numbers as follows

a := complex(1.2, 3.4)
var b complex128 := complex(1.2, 3.4)
c := 8 + 7i

Complex numbers is the same mathematical concept where you had a real and an imaginary number. Both the real and imaginary part are floats either float32 or float64.

Complex numbers can be either 128bit or 64bit

  1. complex128 -> 64bit f real + 64bit imaginary

  2. complex64 -> 32bit real + 32bit imaginary

Zero Values

Following are the zero values, which are given when variables are declared without initializing

  1. 0 for numeric types

  2. false for boolean type

  3. "" for strings

Null

In Golang, null is replaced with nil. We will talk more about nil when discussing pointers and interfaces.

Go Programming Language - Basic To Advance

Part 2 of 5

This series is all about Go Programming Language. We will start from basic features and then discuss about creating large projects with best practices and at the end also cover system design and design patterns using Golang.

Up next

Variables in Go

Variables in go can be declared using following syntax var <variable-name> <data-type> = <value> For e.g. package main func main(){ var x int = 40 } There is also a shortcut of defining variables package main func main(){ x := 10 } In th...

More from this blog

P

Programming, System Design and AI | Latencot

15 posts

Hear, you will read all about tech and tech updates. From writing code to building AI systems, we'll talk and share about everything.