Vivek Anand Sharma
Slim Coder
LOSE YOURSELF
EMINEM
WHEN I'M GONE
MARSHAL
MY NAME IS
SLIM SHADY
// 1st Way
var i int ;
i = 42 ;
// 2nd Way
var j int = 42 ;
// 3rd Way
k:= 42 ;// Boolean
n := 1 == 1
m := 2 == 3
var a bool = true
b := true
// Integer
c := 1
// Floating
e := 1.4543
// String
g := "hello"
h := "world"
// Complex
var i complex128 = 1 + 2i
var j complex128 = complex(1, 2)
b := 1+2i
b := complex(1,2)// Float
const b float32 = 2.4;
// String
const c string = "string constant";
// Boolean
const d bool = true;
// Integer
const a int = 5;
// Complex
const j complex128 = complex(1, 2);// One Dimentional Arrays (Pass By Value By Default)
grades := [3]int{1, 2, 3};
var students [3]string;
students[0] = "Vivek";
students[1] = "Fahad";
students[2] = "Darshan";
a := [...]string{"hello", "world"};
// Multi-Dimentional Arrays
var matrix1 = [3][3]int{[3]int{1, 2, 3}, [3]int{1, 2, 3}, [3]int{1, 2, 3}}
var matrix2 [3][3]int
matrix2[0] = [3]int{1, 2, 3}
matrix2[1] = [3]int{1, 2, 3}
matrix2[2] = [3]int{1, 2, 3}
// Slices (Pass By Reference By Default)
slicea := []int{1, 2, 3}
// Multi-Dimentional Slices
slicea := [][]int{{1,2,3},{1,2,3},{1,2,3}}// Map (Key Value) -> Pass By Reference
a := map[string]string{
"firstName": "Vivek",
"middleName": "Anand",
"lastName": "Sharma",
}
//Structs -> Pass By Value
type Doctor struct {
firstName string
middleName string
lastName string
}
aDoctor := Doctor{
firstName: "Vivek",
middleName: "Anand",
lastName: "Sharma",
}// IF statement
if true {
fmt.Println("The test is true")
}
// IF ELSE IF statement
guessB := 40
if guessB < 30 {
fmt.Println("Too Low")
} else if guessB > 30 {
fmt.Println("Too High")
} else {
fmt.Println("To Equal")
}
// SWITCH Statement
switch 2 {
case 1:
fmt.Println("Case 1")
case 2:
fmt.Println("Case 2")
default:
fmt.Println("Case Default")
}// For Loop
for i := 0; i < 5; i++ {
fmt.Println("Output")
}
// Infinite For Loop
for {
fmt.Println("Infinite Output")
}
// Defer
fmt.Println("start")
defer fmt.Println("middle")
fmt.Println("end")
// Panic
fmt.Println("start")
panic("Something bad happened")
fmt.Println("end")
// Defer , Panic and Recover
fmt.Println("start")
defer func() {
if err := recover(); err != nil {
fmt.Println("Error:", err)
}
}()
panic("something bad happened")
fmt.Println("end")// Pointers
var a int = 45
var b *int = &a
a = 25
fmt.Println(a, *b)func sayMessage(msg string, id int) {
fmt.Println("Message is : ", msg)
fmt.Println("Id is : ", id)
}
// Go program to illustrate the
// method with struct type receiver
package main
import "fmt"
// Author structure
type author struct {
name string
branch string
particles int
salary int
}
// Method with a receiver
// of author type
func (a author) show() {
fmt.Println("Author's Name: ", a.name)
fmt.Println("Branch Name: ", a.branch)
fmt.Println("Published articles: ", a.particles)
fmt.Println("Salary: ", a.salary)
}
// Main function
func main() {
// Initializing the values
// of the author structure
res := author{
name: "Sona",
branch: "CSE",
particles: 203,
salary: 34000,
}
// Calling the method
res.show()
} // Function Syntax
func function_name(parameter_list)(return_type) {
// Code
}
// Method Syntax
func(reciever_name Type) method_name(parameter_list)(return_type){
// Code
}package main
import "fmt"
// Interface => Define Behaviour
type Shape interface {
Area() float64
}
// Struct
type Rect struct {
width float64
height float64
}
// Area Method
func (r Rect) Area() float64 {
return r.width * r.height
}
// Main Function
func main() {
var s Shape
s = Rect{5.0, 4.0}
fmt.Println("Area of Rectangle Is : ", s.Area())
}
package main
// Synchronous package
import (
"fmt"
"sync"
)
// Wait Group
var wg = sync.WaitGroup{}
// Main Function
func main() {
wg.Add(1)
go sayHello()
fmt.Println("Hello World 2");
wg.Wait()
}
// SayHello Function
func sayHello() {
fmt.Println("Hello World 1")
wg.Done()
}package main
import (
"fmt"
"sync"
)
var wg = sync.WaitGroup{}
func main() {
ch := make(chan int)
wg.Add(2)
go func() {
ch <- 42
wg.Done()
}()
go func() {
i := <-ch
fmt.Println(i)
wg.Done()
}()
wg.Wait()
}You can find complete codebase on GitHub.
Don't forget to write your first server on GoLang by using this framework known as gin.
Don't forget to read this article.
Don't forget to install Golang on your system and run all programs which we tested on Golang playgroud.
Don't forget to join Golang Pakistan Group on Facebook.
By Vivek Anand Sharma