Go data types, type casting and constants

Go data types, type casting and constants

Data types, type casting and constants

This blog is in continuation with the previous blog, where I discussed variables and functions in Golang. In this blog, I will be discussing the various data types in Go and the constants. For the previous post, the link is here.

Data types

Data types define what kind of data is stored inside a variable and what operations can be performed on it. For example, if I want to store a number without any fractional part, I will use an integer data type, for real numbers(numbers with fractional part), I will use float/double data type and so on.

List of Data types in Go

Numbers

  1. Integer

    • int, int8, int16, int32, int64

    • uint, uint8, uint16, uint32, uint64, uintptr

  2. Float

    • float32, float64
  3. Complex

    • complex64, complex128
  4. Byte

    • byte (alias for uint8)
  5. Rune

    • rune (alias for int32) for storing unicode character

Text

String

    • string is a sequence of bytes.

Boolean

bool can store either a true or false.

Types to be discussed in later posts

Array

Array denotes a numbered sequence of elements of a single type. Arrays have fixed size. Once created you cannot change the maximum size of the array. Arrays are 0 indexed.

Slice

Slices are just a section of an array. In Go, slices are used for creating dynamic arrays. Slice has size and capacity.

Custom Data type

You can create custom data types by combining different data types using struct, I will discuss the struct in the upcoming blog posts.

Using data types

In Go, while creating the variable, after writing the variable name we write the data type that can be any of the above-mentioned as well as user-defined structs.

import "fmt"
func main(){
    var age uint = 16
//  age is a value that can hold a uint value
    var name string = "Rishabhdev"
//  name is a variable that holds sequence of characters(string)
    var pi float64 = 3.14
//  pi is a variable of float64 type
    var isStudent bool = true
    fmt.Println("Age:",age)
    fmt.Println("Name:",name)
    fmt.Println("is Student:",isStudent)
    fmt.Println("Value of Pi:",pi)
}

Similarly, you can create a variable with any data type.

Constants

Constants are the named values that cannot be changed. The rules for naming constants are the same as variables, usually, constants' names are capitalized to differentiate them from the variables. If you provide the data type to a constant then it is called a Typed constant and if you do not provide the data type to the constant then it is an Untyped constant. Constants are declared using the const keyword then followed by the constant name and type.

Typed constant syntax

const <name> <type> = <value>

Untyped constant syntax

const <name> = <value>

Using constants

The following code shows how to create constants in Go.

Typed constants example

const TEAM_SIZE uint = 12

func main(){
    fmt.Println("Maximum number of players in the Team:",TEAM_SIZE)
}

Untyped constant example

func main(){
    const PI = 3.14159265359
    fmt.Println(PI)
}

Conclusion

Having knowledge of data types is very important. Using constants is also important to stop certain values from changing. In the upcoming blog, I will be discussing Operators in Go. I will be showing you different kinds of operators in Go namely arithmetic, comparison, logical, assignment and bitwise operators.

Keep supporting me by reacting and commenting on my blog posts. This encourages me to keep on writing more blogs.

Link to previous blog is here

Follow, like, share and subscribe to the newsletter for upcoming blog posts.

Did you find this article valuable?

Support Rishabh Kumar Bahukhandi by becoming a sponsor. Any amount is appreciated!