Introduction to Golang

Introduction to Golang

ยท

4 min read

Go Language Introduction

Golang commonly referred as "Go".

  • Go is a programming language which is developed by Google in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson. It is developed with a vision of high performance and fast dvelopment.

  • Go is statically-typed language.

  • Go has a similar syntax to C.
  • Go provides type safety, garbage collection, dynamic-typing capability, many advance built-in types such as variable length arrays and key-value maps etc.
  • Go is modern, fast and comes with powerful standard library.
  • Go has build-in concurrency.
  • Go uses interfaces as the building blocks of code reusability.

Basic structure of Go programs

  1. Package Declaration
  2. Import Packages
  3. Variables
  4. Statements and Expressions
  5. Functions

Install Go in Windows

Step 1 :- Go to Golang official website in their download section https://go.dev/dl/

image.png

Step 2 :- Download the msi installer for windows

image.png

Step 3 :- Run the installer and allow for the permission if it ask for any.

image.png

Step 4 :- Go to your command prompt (CMD) or Power shell to check go is properly installed. Check go version by command go version

image.png

Hurray!! ๐Ÿฅณ๐Ÿฅณ Go is installed in our Windows machine.

Go Hello World Program

while learning a new language we always write hello world program first let's do it with go also.

package main
import "fmt"
func main()  {
    fmt.Println("Hello World")
}

Let's understand the above code

  • The first line is a package declaration, here the name of the package is main. Remember-- package declaration is mandatory in all go programs.
  • second line is import statement, here we are importing "fmt". package. The go compiler will include the files of package fmt.
  • The third line is main function it is where from all execution begins.
  • The last line fmt.Println("Hello World) is a function already available in Go. It will going to print Hello World on the screen.

Running the Go program

To run any go program

  • The file must be saved with any name but the extension must be .go for example HelloWorld.go
  • To run the program saved in file open terminal or command prompt >>>> go to the location where your file is save >>>> Run go run FileName.go for example go run HelloWorld.go

Output

image.png

Go Variables and Data Types

Variable is nothing but a name given to a memory location where our data is stored.

Data type is the type of data stored in the memory like integer, float, string, etc.

  • Variables can be of different types like int, float, struct, slice or it can be of the interface.

To declare a variable in go we use keyword var:
Syntax :-

var identifier type

Example :-

var i int

var f float

var str string
  • When a variable is declared with var it automatically initialize it to the zero-value defined for its type.
  • A type defines the set of values and the set of operations that can take place on those values.

Go Data type example :-

package main

import "fmt"

func main() {
    var i int = 23
    var f float64 = 15.8
    var b bool = true
    var s string = "Gautam"

    fmt.Println(i)
    fmt.Println(f)
    fmt.Println(b)
    fmt.Println(s)

}

Output

image.png

Let's do some experiment with code and print the values without assigning any values to them.

package main

import "fmt"

func main() {
    var i int
    var f float64
    var b bool
    var s string

    fmt.Println(i)
    fmt.Println(f)
    fmt.Println(b)
    fmt.Println(s)

}

Output :-

image.png

Hey! see the default value of every datatype is printed. ๐Ÿ˜ƒ๐Ÿ˜ƒ

Let's do something else๐Ÿค”๐Ÿค” hmmm, what about printing the data type on the console, although we already know that but still

package main

import "fmt"

func main() {
    var i int = 9
    var f float64 = 4.6
    var b bool = true
    var s string = "Gautam Jha"

    fmt.Printf("%T\n", i)
    fmt.Printf("%T\n", f)
    fmt.Printf("%T\n", b)
    fmt.Printf("%T\n", s)

}

Output

image.png

Go Packages and import

packages

Packages are used to categorize our program so that it can be easy to maintain.

  • Every go file belongs to some package. Each Go application must have "main" package so that it can be compiled.
  • An application can consist of different packages. Many different .go file can belong to one main package.
  • The package name must be written in lower case
  • If the packaged is changed and recompiled, all the client program that use this package must be recompile too.

import

  • Go program is linked with different packages though the import keyword.
  • The package names are enclosed within double quotes "".
  • Import loads the public declarations from the compiled package. It dos not import the source code.

There are different ways to import package

  1. In separate statement like :-
    import "fmt"
    import "os"
    

OR

import "fmt"; import "os"

2.There is a shorter way also

import (
    "fmt"
    "os"
)

That's it for this blog, In my next blog we will see control flow statements in Go like loops, if-else, switch statements and many more.

Stay updated. ๐Ÿ˜Š๐Ÿ˜Š

Thankyou for reading.

Did you find this article valuable?

Support Gautam Jha by becoming a sponsor. Any amount is appreciated!

ย