Introduction to Golang

Thoughts. Experiences. Inspiration.
I’m so glad you’ve arrived. gautamjha.hashnode.dev is where I share with you what interests me most, sparking your excitement so that you can nurture your own passions and projects. I hope you enjoy my blogs and all of the content I create. We all need something to motivate us. Take a look around; perhaps you’ll discover what exhilarates you. Are you ready to be inspired?
I am BCA Student, I am learning DevOps and also applying it on my personal projects. I want to collaborate with other folks and make some amazing team to learn from them, with them.
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
- Package Declaration
- Import Packages
- Variables
- Statements and Expressions
- Functions
Install Go in Windows
Step 1 :- Go to Golang official website in their download section https://go.dev/dl/

Step 2 :- Download the msi installer for windows

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

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

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 functionit 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
.gofor exampleHelloWorld.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.gofor examplego run HelloWorld.go
Output

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

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 :-

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

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
- 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.




