GOLANG FOR ABSOLUTE BEGINNERS

AGENDA

  • Introduction of Speaker
  • Introduction of Golang
  • Variables
  • Primitives
  • Contants
  • Arrays And Slices
  • Maps And Structs
  • Control Flow
  • Looping
  • Defer , Panic And Recover
  • Pointers
  • Functions
  • Methods
  • Functions V/S Methods
  • Interfaces 
  • Goroutines
  • Channels

INTRODUCTION

OF SPEAKER

I'm Vivek

  • Software Engineer at Trukkr
  • Community Lead of Golang Pakistan.
  • Communiy Manager of MWB
  • Co-Founder of DevCreatives++.
  • Founded 2 Top GitHub Organizations.
  • Writing For 3 Publishers on Medium.
  • Member of AWS and Linux Pakistan.

I'M ON THESE PLACES

 I'M A.K.A SLIM CODER 

IT'S ALL START WITH

IT'S ALL START WITH EM

LOSE YOURSELF

EMINEM

WHEN I'M GONE

MARSHAL

MY NAME IS

SLIM SHADY

SLIM IS ON THESE PLACES

IT'S ALL START WITH

WANT TO LEARN MORE

 

INTRODUCTION OF GOLANG

  • Golang was created by 3 software engineer of Google namely Robert ,  Rob and Ken.

WHO CREATED GOLANG ?

 
  • There was some limitation Google running into that's why they decide to create a new language.
  • Before Golang , Google was using 3 languages i.e Python , Java and C/C++ but still there was some limitation with these languages.

WHY GOLANG ?

 
  • Python was easy to use but slow at the same time because it is an interpreted language.
  • Java was quick but its type system was complex for distrubing system.
  • C/C++ was quick but its type system was complex for distrubing system and it have slow compiles times.

WHAT IS GOLANG ?

 
  • Strong and Statically Typed
 
  • Awesome Community
 
  • Simplicity
 
  • Fast Compiles Times
 
  • Garbage Collected
 
  • Built-In Concurrency
 
  • Compile To Standalone Library
 

VARIABLES

 

VARIABLES

 
// 1st Way
var i int ; 
i = 42 ;

// 2nd Way
var j int = 42 ;

// 3rd Way
k:= 42 ;

PRIMITIVES

 

PRIMITIVES

 
// 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)

CONSTANTS

 

CONSTANTS

 
// 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);

ARRAYS AND SLICES

 

ARRAYS AND SLICES

 
// 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 AND STRUCTS

 

MAP AND STRUCTS

 
// 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",
}

CONTROL FLOW

 

CONTROL FLOW

 
// 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")
}

LOOPING

 

LOOPING

 
// For Loop
for i := 0; i < 5; i++ {
		fmt.Println("Output")
}

// Infinite For Loop
for {
	fmt.Println("Infinite Output")
}

DEFER, PANIC AND RECOVER

 

DEFER, PANIC AND RECOVER

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

 

POINTERS

 
// Pointers
var a int = 45
var b *int = &a
a = 25
fmt.Println(a, *b)

FUNCTIONS

 

FUNCTIONS

 
func sayMessage(msg string, id int) {
	fmt.Println("Message is : ", msg)
	fmt.Println("Id is : ", id)
}

METHODS

 

METHODS

 
// 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() 
} 

FUNCTIONS VS METHODS

 

FUNCTIONS V/S METHODS

 
// Function Syntax

func function_name(parameter_list)(return_type) {
// Code
}

// Method Syntax

func(reciever_name Type) method_name(parameter_list)(return_type){
// Code
}

INTERFACES

 

INTERFACES

 
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())
}

GOROUTINES

 
 

GOROUTINES

 
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()
}

CHANNELS

 
 

CHANNELS

 
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()
}

WANT TO LEARN MORE

 
 

You can find complete codebase on GitHub.

SUGGESTIONS

 
 

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.

READY TO JOIN GOLANG PAKISTAN

 
 

Don't forget to join Golang Pakistan Group on Facebook.

WANTS TO JOIN MENTORS WITHOUT BORDERS?

If yes! then don't forget to send me an email: vivek@mentorswithoutborders.net

WANTS TO JOIN LINUX PAKISTAN?

If yes! then don't forget to send me an email: vivek@linuxpakistan.org

Golang For Absolute Beginners - DSC UoS

By Vivek Anand Sharma

Golang For Absolute Beginners - DSC UoS

  • 107