๐Ÿš€ Go Variables and Data types

Basics of go variables and data types

ยท

4 min read

๐Ÿš€ Go Variables and Data types

๐Ÿ‘‹ Hi everyone and welcome ...

In this article i'll explain go variables and Data types, let's start

What is Go?

Go is a statically typed, compiled programming language designed at Google by Robert Griesemer, Rob Pike, and Ken Thompson, it's expressive, concise, clean, and efficient. Its concurrency mechanisms make it easy to write programs that get the most out of multicore and networked machines, while its novel type system enables flexible and modular program construction. Go compiles quickly to machine code yet has the convenience of garbage collection and the power of run-time reflection. It's a fast, statically typed, compiled language that feels like a dynamically typed, interpreted language.

Variables

A variable is a piece of storage containing data temporarily to work with it (Like a bag).

To declare a variable or (Multiple Variables) in Golang use var keyword.

Syntax

var your-variable-name data-type

Syntax of constant variable

const your-variable-name data-type

package main

func main() {
    // String data type
    var string_variable string

    // Byte data type
    var byte_variable byte

    // Integer data type
    var int_variable int

    // Float data type
    var float_variable float32

    // Boolean data type
    var boolean_variable bool

    // Complex numbers data type
    var complex_variable complex64

    // Constant variable
    const pi float32 = 3.14

}

๐Ÿ’ก You can use this short way to declare variable by :=

username, password := "idev_io", 11111

๐Ÿ’ก Variables declaration can be grouped together into blocks for greater readability and code quality.

var (
        name  = "IDev_IO"
        email = "idev_io@email.com"
        job   = "Developer"
)

๐Ÿ’ก To assign values to variables use = .

package main

func main() {
    var string_variable string
    var byte_variable byte
    var int_variable int
    var float_variable float32
    var boolean_variable bool
    var complex_variable complex64

    string_variable = "Go Article by IDev_IO programmer"
    byte_variable = 21
    int_variable = 100
    float_variable = 90.08
    boolean_variable = true
    complex_variable = complex(3, 2) // complex(real part, imaginary part)
}

What if you don't assign values to variables?

If you declare a variable without assigning it a value, GoLang will automatically bind a default value to the variable Null(Nothing) for string, 0 for byte, int, float, false for boolean, 0+0i for complex.

๐Ÿ’ก To print values to console use fmt package you must import it.

๐Ÿ’ก Use Print() function to print value of variables with default formats.

package main

// Required packages
import (
    "fmt"
)

func main() {
    var string_variable string
    var byte_variable byte
    var int_variable int
    var float_variable float32
    var boolean_variable bool
    var complex_variable complex64

    string_variable = "Go Article by IDev_IO programmer"
    byte_variable = 21
    int_variable = 100
    float_variable = 90.08
    boolean_variable = true
    complex_variable = complex(3, 2) // complex(real part, imaginary part)

    fmt.Print(string_variable)
    fmt.Print(byte_variable)
    fmt.Print(int_variable)
    fmt.Print(float_variable)
    fmt.Print(boolean_variable)
    fmt.Print(complex_variable)
}

๐Ÿ’ก Or you can use Println() function print value of variables but inserts a new line at the end.

package main

// Required packages
import (
    "fmt"
)

func main() {
    var string_variable string
    var byte_variable byte
    var int_variable int
    var float_variable float32
    var boolean_variable bool
    var complex_variable complex64

    string_variable = "Go Article by IDev_IO programmer"
    byte_variable = 21
    int_variable = 100
    float_variable = 90.08
    boolean_variable = true
    complex_variable = complex(3, 2) // complex(real part, imaginary part)

    fmt.Println(string_variable)
    fmt.Println(byte_variable)
    fmt.Println(int_variable)
    fmt.Println(float_variable)
    fmt.Println(boolean_variable)
    fmt.Println(complex_variable)
}

๐Ÿ’ก Or you can use Printf() function to print the formatted output.

package main

// Required packages
import (
    "fmt"
)

func main() {
    var string_variable string
    var byte_variable byte
    var int_variable int
    var float_variable float32
    var boolean_variable bool
    var complex_variable complex64

    string_variable = "Go Article by IDev_IO programmer"
    byte_variable = 21
    int_variable = 100
    float_variable = 90.08
    boolean_variable = true
    complex_variable = complex(3, 2) // complex(real part, imaginary part)

    // \n New Line
    // %s for string
    // %d for integer base 10
    // %g for float32, complex64 
    // %t for boolean true or false

    fmt.Printf("String: %s \nByte: %d \nInteger: %d \nFloat: %g \nBoolean: %t \nComplex: %g",
        string_variable, byte_variable, int_variable, float_variable,
        boolean_variable, complex_variable)
}

I hope you gained something here today and reading it was worth the time.

Stay tuned for next articles Good Luck ๐Ÿ˜Š.

ย