SlideShare a Scribd company logo
1 of 17
Download to read offline
burrowing through Go!!
Language Basics
Vishal Ghadge
Sourav Ray
Prerequisites
1. Installed go on your laptop
2. Set your GOROOT and GOPATH
3. Installed git and Hg
Hello world…
package main
import "fmt"
func main() {
fmt.Println("Hello, 世界")
}
Datatypes
Types:
bool byte complex64 complex128 error float32 float64
int int8 int16 int32 int64 rune string
uint uint8 uint16 uint32 uint64 uintptr
Constants:
true false iota ← interesting..
Zero value:
nil
Variables
Different ways to define and initializing variables
var counter int
var name string
var(
counter int
name string
)
counter, name := 10, "Golang"
var x int ← Global
func main() {
x = 10 ← Global
fmt.Println(x)
function()
}
func function() {
// x = 4 ← Global
x := 5 ← Local
fmt.Println(x)
}
Datatypes
iota: Enumeration in Golang
const (
const0 = iota
const1 = iota
const2 = iota
)
const (
const0 = iota
const1
const2
)
const (
a = 1 << iota
b = 1 << iota
c = 1 << iota
)
const (
a = 1 << iota
b
c
)
const(
bit0, mask0 = 1 << iota, 1<<iota - 1 // bit0 == 1, mask0 == 0
bit1, mask1
_, _ // skips iota == 2
bit3, mask3
)
Control statement
if-else block
if x > 0 {
return y
}else{
return z
}
if with initialization:
x := 1
if r := x % 2; r == 0 {
fmt.Println("Even", r)
} else {
fmt.Println("Odd", r)
}
Control statement
For Loop:
for init; condition; post { } ← c like for loop
for condition { } ← while loop
for { } ← Endless loop
J: for j := 0; j < 5; j++ {
for i := 0; i < 10; i++ {
if i > 5 {
break J
}
println(i)
}
}
list := []string{"a", "b", "c", "d", "e", "f"}
for k, v := range list {
fmt.Println(k, v)
}
Control statement
Switch case:
● No break statement
● fallthrough keyword
● “,” as or for case statement.
i := 4
switch i {
case 0:
case 2:
case 4:
fallthrough
case 8:
fmt.Println("Even")
case 1, 3, 5, 7, 9:
fmt.Println("Odd")
default:
fmt.Println("???")
}
Type declaration
Array type:
var arr[10]int
a:= [3]int{1,2,3}
weekEnd:=[2]string{“saturday”, “sunday”}
c:=[2][2]int{[2]int{1,2},[2]int{3,4}}
Type declaration
Slice
sl := make([]int, 10)
a:=[...]int{1,2,3,4,5} //array
s1:=a[m:n]
m ← starting index
n ← last index n-1
n-m ← length
//copy, append
s0 := []int{0, 1, 3, 4}
s1 := append(s0, 5) //returns new slice
n:=copy(s1,s[0,4]) //copy within limits
Type declaration
Struct:
type Circle struct {
x, y, r float64
}
var c Circle
c := Circle{x: 0, y: 0, r: 5}
c := shape.Circle{0, 0, 5}
//Embedded types
type Shape struct {}
func (s *Shape) Draw() {
fmt.Println("Draw like shape")
}
type Circle struct {
x, y, r float64
Shape
}
Type declaration
Interface:
type doubleMaker interface {
double()
}
type intImplementer int
func (i intImplementer) double() {
fmt.Println(2 * i)
}
//Embedded types
type tripleMaker interface {
triple()
}
type doubleMaker interface {
tripleMaker
double()
}
Type declaration
Functions:
● Scope
● Multiple return values
● Named result parameters
● Deferred code
● function as a value
● callbacks
Goroutines and channels
Code sample..
ready(“tea”,1) ← normal function call
go ready(“tea”,1) ← started as goroutine
ci := make(chan int) ← unbuffered channel of integers
cj := make(chan int, 0) ← unbuffered channel of integers
cs := make(chan int, 100) ← buffered channel of integers
Discussions...
Resources
Just search for golang...

More Related Content

What's hot

PHP in 2018 - Q4 - AFUP Limoges
PHP in 2018 - Q4 - AFUP LimogesPHP in 2018 - Q4 - AFUP Limoges
PHP in 2018 - Q4 - AFUP Limoges✅ William Pinaud
 
The Ring programming language version 1.3 book - Part 12 of 88
The Ring programming language version 1.3 book - Part 12 of 88The Ring programming language version 1.3 book - Part 12 of 88
The Ring programming language version 1.3 book - Part 12 of 88Mahmoud Samir Fayed
 
Double linked list
Double linked listDouble linked list
Double linked listSayantan Sur
 
「Frama-Cによるソースコード検証」 (mzp)
「Frama-Cによるソースコード検証」 (mzp)「Frama-Cによるソースコード検証」 (mzp)
「Frama-Cによるソースコード検証」 (mzp)Hiroki Mizuno
 
โปรแกรมย่อยและฟังชันก์มาตรฐาน
โปรแกรมย่อยและฟังชันก์มาตรฐานโปรแกรมย่อยและฟังชันก์มาตรฐาน
โปรแกรมย่อยและฟังชันก์มาตรฐานknang
 
Array imp of list
Array imp of listArray imp of list
Array imp of listElavarasi K
 
为什么 rust-lang 吸引我?
为什么 rust-lang 吸引我?为什么 rust-lang 吸引我?
为什么 rust-lang 吸引我?勇浩 赖
 
A "Do-It-Yourself" Specification Language with BeepBeep 3 (Talk @ Dagstuhl 2017)
A "Do-It-Yourself" Specification Language with BeepBeep 3 (Talk @ Dagstuhl 2017)A "Do-It-Yourself" Specification Language with BeepBeep 3 (Talk @ Dagstuhl 2017)
A "Do-It-Yourself" Specification Language with BeepBeep 3 (Talk @ Dagstuhl 2017)Sylvain Hallé
 
Circular linked list
Circular linked listCircular linked list
Circular linked listSayantan Sur
 
Single linked list
Single linked listSingle linked list
Single linked listSayantan Sur
 
What is python
What is pythonWhat is python
What is pythonEU Edge
 

What's hot (20)

PHP in 2018 - Q4 - AFUP Limoges
PHP in 2018 - Q4 - AFUP LimogesPHP in 2018 - Q4 - AFUP Limoges
PHP in 2018 - Q4 - AFUP Limoges
 
The Ring programming language version 1.3 book - Part 12 of 88
The Ring programming language version 1.3 book - Part 12 of 88The Ring programming language version 1.3 book - Part 12 of 88
The Ring programming language version 1.3 book - Part 12 of 88
 
Double linked list
Double linked listDouble linked list
Double linked list
 
Git avançado
Git avançadoGit avançado
Git avançado
 
Functional php
Functional phpFunctional php
Functional php
 
Avl tree
Avl treeAvl tree
Avl tree
 
「Frama-Cによるソースコード検証」 (mzp)
「Frama-Cによるソースコード検証」 (mzp)「Frama-Cによるソースコード検証」 (mzp)
「Frama-Cによるソースコード検証」 (mzp)
 
โปรแกรมย่อยและฟังชันก์มาตรฐาน
โปรแกรมย่อยและฟังชันก์มาตรฐานโปรแกรมย่อยและฟังชันก์มาตรฐาน
โปรแกรมย่อยและฟังชันก์มาตรฐาน
 
Circular queue
Circular queueCircular queue
Circular queue
 
Array imp of list
Array imp of listArray imp of list
Array imp of list
 
为什么 rust-lang 吸引我?
为什么 rust-lang 吸引我?为什么 rust-lang 吸引我?
为什么 rust-lang 吸引我?
 
week-17x
week-17xweek-17x
week-17x
 
C++ programs
C++ programsC++ programs
C++ programs
 
A "Do-It-Yourself" Specification Language with BeepBeep 3 (Talk @ Dagstuhl 2017)
A "Do-It-Yourself" Specification Language with BeepBeep 3 (Talk @ Dagstuhl 2017)A "Do-It-Yourself" Specification Language with BeepBeep 3 (Talk @ Dagstuhl 2017)
A "Do-It-Yourself" Specification Language with BeepBeep 3 (Talk @ Dagstuhl 2017)
 
งานนำเสนอ อาจารย์ลาวัลย์
งานนำเสนอ อาจารย์ลาวัลย์งานนำเสนอ อาจารย์ลาวัลย์
งานนำเสนอ อาจารย์ลาวัลย์
 
Circular linked list
Circular linked listCircular linked list
Circular linked list
 
Single linked list
Single linked listSingle linked list
Single linked list
 
What is python
What is pythonWhat is python
What is python
 
week-18x
week-18xweek-18x
week-18x
 
Unity Programing on Boo
Unity Programing on BooUnity Programing on Boo
Unity Programing on Boo
 

Viewers also liked

50 very difficult shots
50 very difficult shots50 very difficult shots
50 very difficult shotsRay-Hsin Chang
 
a little astronomy from hubble
a little astronomy from hubblea little astronomy from hubble
a little astronomy from hubbleRay-Hsin Chang
 
Healthy schools inc amoroso, nyl jarem 15
Healthy schools inc   amoroso, nyl jarem 15Healthy schools inc   amoroso, nyl jarem 15
Healthy schools inc amoroso, nyl jarem 15Arem Amoroso
 
Digital citizenship
Digital citizenshipDigital citizenship
Digital citizenshiphvuj
 
Digital citizenship
Digital citizenshipDigital citizenship
Digital citizenshiphvuj
 
Zhou jiansen unesco worldheritage
Zhou jiansen unesco worldheritageZhou jiansen unesco worldheritage
Zhou jiansen unesco worldheritageRay-Hsin Chang
 

Viewers also liked (7)

50 very difficult shots
50 very difficult shots50 very difficult shots
50 very difficult shots
 
a little astronomy from hubble
a little astronomy from hubblea little astronomy from hubble
a little astronomy from hubble
 
3 pencarian buta
3 pencarian buta3 pencarian buta
3 pencarian buta
 
Healthy schools inc amoroso, nyl jarem 15
Healthy schools inc   amoroso, nyl jarem 15Healthy schools inc   amoroso, nyl jarem 15
Healthy schools inc amoroso, nyl jarem 15
 
Digital citizenship
Digital citizenshipDigital citizenship
Digital citizenship
 
Digital citizenship
Digital citizenshipDigital citizenship
Digital citizenship
 
Zhou jiansen unesco worldheritage
Zhou jiansen unesco worldheritageZhou jiansen unesco worldheritage
Zhou jiansen unesco worldheritage
 

Similar to Burrowing through go! the book

Similar to Burrowing through go! the book (20)

Go Lang Tutorial
Go Lang TutorialGo Lang Tutorial
Go Lang Tutorial
 
Let's golang
Let's golangLet's golang
Let's golang
 
Clean Code Development
Clean Code DevelopmentClean Code Development
Clean Code Development
 
Are we ready to Go?
Are we ready to Go?Are we ready to Go?
Are we ready to Go?
 
Introduction to typescript
Introduction to typescriptIntroduction to typescript
Introduction to typescript
 
Go ahead, make my day
Go ahead, make my dayGo ahead, make my day
Go ahead, make my day
 
TypeScript - All you ever wanted to know - Tech Talk by Epic Labs
TypeScript - All you ever wanted to know - Tech Talk by Epic LabsTypeScript - All you ever wanted to know - Tech Talk by Epic Labs
TypeScript - All you ever wanted to know - Tech Talk by Epic Labs
 
All I know about rsc.io/c2go
All I know about rsc.io/c2goAll I know about rsc.io/c2go
All I know about rsc.io/c2go
 
DataTypes.ppt
DataTypes.pptDataTypes.ppt
DataTypes.ppt
 
Getting started cpp full
Getting started cpp   fullGetting started cpp   full
Getting started cpp full
 
C tutorial
C tutorialC tutorial
C tutorial
 
Python-GTK
Python-GTKPython-GTK
Python-GTK
 
Whats new in_csharp4
Whats new in_csharp4Whats new in_csharp4
Whats new in_csharp4
 
Golang and Eco-System Introduction / Overview
Golang and Eco-System Introduction / OverviewGolang and Eco-System Introduction / Overview
Golang and Eco-System Introduction / Overview
 
golang_getting_started.pptx
golang_getting_started.pptxgolang_getting_started.pptx
golang_getting_started.pptx
 
Python
PythonPython
Python
 
Vcs16
Vcs16Vcs16
Vcs16
 
Lập trình C
Lập trình CLập trình C
Lập trình C
 
C programs Set 4
C programs Set 4C programs Set 4
C programs Set 4
 
Mcq cpup
Mcq cpupMcq cpup
Mcq cpup
 

Recently uploaded

"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 

Recently uploaded (20)

"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 

Burrowing through go! the book

  • 1. burrowing through Go!! Language Basics Vishal Ghadge Sourav Ray
  • 2. Prerequisites 1. Installed go on your laptop 2. Set your GOROOT and GOPATH 3. Installed git and Hg
  • 3. Hello world… package main import "fmt" func main() { fmt.Println("Hello, 世界") }
  • 4. Datatypes Types: bool byte complex64 complex128 error float32 float64 int int8 int16 int32 int64 rune string uint uint8 uint16 uint32 uint64 uintptr Constants: true false iota ← interesting.. Zero value: nil
  • 5. Variables Different ways to define and initializing variables var counter int var name string var( counter int name string ) counter, name := 10, "Golang" var x int ← Global func main() { x = 10 ← Global fmt.Println(x) function() } func function() { // x = 4 ← Global x := 5 ← Local fmt.Println(x) }
  • 6. Datatypes iota: Enumeration in Golang const ( const0 = iota const1 = iota const2 = iota ) const ( const0 = iota const1 const2 ) const ( a = 1 << iota b = 1 << iota c = 1 << iota ) const ( a = 1 << iota b c ) const( bit0, mask0 = 1 << iota, 1<<iota - 1 // bit0 == 1, mask0 == 0 bit1, mask1 _, _ // skips iota == 2 bit3, mask3 )
  • 7. Control statement if-else block if x > 0 { return y }else{ return z } if with initialization: x := 1 if r := x % 2; r == 0 { fmt.Println("Even", r) } else { fmt.Println("Odd", r) }
  • 8. Control statement For Loop: for init; condition; post { } ← c like for loop for condition { } ← while loop for { } ← Endless loop J: for j := 0; j < 5; j++ { for i := 0; i < 10; i++ { if i > 5 { break J } println(i) } } list := []string{"a", "b", "c", "d", "e", "f"} for k, v := range list { fmt.Println(k, v) }
  • 9. Control statement Switch case: ● No break statement ● fallthrough keyword ● “,” as or for case statement. i := 4 switch i { case 0: case 2: case 4: fallthrough case 8: fmt.Println("Even") case 1, 3, 5, 7, 9: fmt.Println("Odd") default: fmt.Println("???") }
  • 10. Type declaration Array type: var arr[10]int a:= [3]int{1,2,3} weekEnd:=[2]string{“saturday”, “sunday”} c:=[2][2]int{[2]int{1,2},[2]int{3,4}}
  • 11. Type declaration Slice sl := make([]int, 10) a:=[...]int{1,2,3,4,5} //array s1:=a[m:n] m ← starting index n ← last index n-1 n-m ← length //copy, append s0 := []int{0, 1, 3, 4} s1 := append(s0, 5) //returns new slice n:=copy(s1,s[0,4]) //copy within limits
  • 12. Type declaration Struct: type Circle struct { x, y, r float64 } var c Circle c := Circle{x: 0, y: 0, r: 5} c := shape.Circle{0, 0, 5} //Embedded types type Shape struct {} func (s *Shape) Draw() { fmt.Println("Draw like shape") } type Circle struct { x, y, r float64 Shape }
  • 13. Type declaration Interface: type doubleMaker interface { double() } type intImplementer int func (i intImplementer) double() { fmt.Println(2 * i) } //Embedded types type tripleMaker interface { triple() } type doubleMaker interface { tripleMaker double() }
  • 14. Type declaration Functions: ● Scope ● Multiple return values ● Named result parameters ● Deferred code ● function as a value ● callbacks
  • 15. Goroutines and channels Code sample.. ready(“tea”,1) ← normal function call go ready(“tea”,1) ← started as goroutine ci := make(chan int) ← unbuffered channel of integers cj := make(chan int, 0) ← unbuffered channel of integers cs := make(chan int, 100) ← buffered channel of integers