Golang Basics series #2

4 min read

Basic data type in Golang Number,string, Boolean with simple examples

Data Types in Go

Data types specify the type of data that a valid Go variable can hold. In Go language, the type is divided into four categories which are as follows:

  1. Basic type: Numbers, strings, and Booleans come under this category.
  2. Aggregate type: Array and structs come under this category.
  3. Reference type: Pointers, slices, maps, functions, and channels come under this category.
  4. Interface type

Here, we will discuss Basic Data Types in the Go language. The Basic Data Types are further categorized into three subcategories which are: 

  • Numbers
  • Booleans
  • Strings

About Booleans

Booleans in Go are represented by the bool type, which values can be either true or false.

Go supports three boolean operators: ! (NOT), && (AND), and || (OR).

!true && false  // Output: false 
!(true && false) // Output: true

About Numbers

Go contains basic numeric types that can represent sets of either integer or floating-point values. There a different types depending on the size of value you require and the architecture of the computer where the application is running (e.g. 32-bit and 64-bit).

This concept will concentrate on two number types:

  • int: e.g. 0, 255, 2147483647. A signed integer that is at least 32 bits in size (value range of: -2147483648 through 2147483647). But this will depend on the systems architecture. Most modern computers are 64 bit, therefore int will be 64 bits in size (value rate of: -9223372036854775808 through 9223372036854775807).
  • float64: e.g. 0.0, 3.14. Contains the set of all 64-bit floating-point numbers.

Go supports the standard set of arithmetic operators of +, -, *, / and % (remainder not modulo). In Go, assignment of a value between different types requires explicit conversion. For example, to convert an int to a float64 you would need to do the following:

var x int = 42
f := float64(x)
 fmt.Printf("x is of type: %s\n", reflect.TypeOf(x))
 // Output: x is of type: int 
fmt.Printf("f is of type: %s\n", reflect.TypeOf(f))
 // Output: f is of type: float64

About Strings

A string in Go is an immutable sequence of bytes, which don't necessarily have to represent characters. A string literal is defined between double quotes:

const name = "Jane"

Some values need to be escaped:

Some values need to be escaped:

Value Description

\a Alert or bell

\b Backspace

\\ Backslash

\t Horizontal tab

\n Line feed or newline

\f Form feed

\r Carriage return

\v Vertical tab

\' Single quote

\" Double quote

const daltons = "Joe\nWilliam\nJack\nAverell"

Raw string literals use backticks (`) as their delimiter instead of double quotes and are interpreted literally, meaning that there is no need to escape characters or newlines.

const daltons = `Joe

those are the basics of data types in golang we will go to the advance data types on upcoming series

if you find this blog valuable don't forget to clap and subscribe

part one link:


Comments (0)
No comments yet