Data Types in Go


GOLang Data Types

Data types are one of the key things one has to know about while learning a language. They constitute the base of any programming language. I will be covering major data types that any programmer would be using throughout the day while working with GO.
Without wasting enough time lets start with GO data types.

Basic Types

  • int
  • float32
  • float64
  • bool
  • strings
  • derived types

Int

Ints are used to store 32 or 64 bits integer values defined in GO as follows:

var i int = 122
i:=122

The latter syntax is used when we want GO Compiler to pick the data type by inferring the value stored in the variable.

Floats

Floats are used to store decimal values in Go. Floats can be of two types
  1. float32
  2. float64
These two types correspond to the amount of space 32 bytes and 64 bytes respectively to store the numeric values.
floats can be created in Go using the following syntax:

var f float32 = 1.1 var f64 float64 = 1.1

Bool

Bool data type is used to store the boolean i.e true/false values in GO.
The following syntax can be used to create the booleans in Go

var b bool = true

Strings

Strings are data types used to store non-numeric values like names, address, positions, etc.
GO. Strings in Go are immutable types, which means that strings once created can not be changed. This doesn't mean you can not alter the value of a string, instead every time you alter the value fo string a new string is created.

var s string = "Japneet"

Derived Types

GO has vast varieties of derived types like
  • Arrays
  • Slices
  • Structs
  • Interfaces
  • Maps
which I will be talking about in the next blog. 


Here is a shortcode for data types covered in this blog
package main
import "fmt"
func main() {
   var i int = 122
   var f float32 = 1.1
   var f64 float64 = 1.1
   var b bool = true
   var s string = "Japneet"
   fmt.Printf("%T\n", i)
   fmt.Printf("%T\n", f)
   fmt.Printf("%T\n", f64)
   fmt.Printf("%T\n", b)
   fmt.Printf("%T\n", s)
}

If you have a problem understanding this code, go through my previous blog on getting started with go.

Stay tuned for the next blog.
Happy Learning...


Comments

Popular posts from this blog

Spidering the web with Python

Word Vectorization

Machine Learning -Solution or Problem