SlideShare ist ein Scribd-Unternehmen logo
1 von 23
Downloaden Sie, um offline zu lesen
coding in GO
Monthly meetup November 2015 NSBM
Raveen Perera
History
Created by Robert Griesemer, Rob Pike, Ken Thompson
Developed in 2007 and first stable open source release 2009 (BSD)
GO
Fast, compiled language, directly to machine code and spearheaded by
What’s so special about GO ?
Compilation
Very fast compilation (seconds)
No VM needed
GOs Assembler
Tools
go fmt go vet
go test go doc
Concurrency
Asynchronous processes called
GOroutines
Channels used to pass data
between routines
Standard Library
net/http
flag
encoding/json encoding/xml
Simplicity in Syntax
GO stands between C and Python
Highly influenced by many other
popular programming languages
Deployment
Can be compiled to a single binary
file
You can build and compile in your
host or server
Let’s dive in
Download and install GO
https://golang.org/dl/
Download and install Sublime Text 3
http://www.sublimetext.com/3
and install GOSublime plugin
https://github.com/DisposaBoy/GoSublime
package main
import (
"fmt"
)
func main() {
fmt.Println("Hello World!")
}
$ go run helloworld.go
The Basics
https://golang.org/ref/spec
Variables
var age int = 40
name := “John Doe”
const pi float64 = 3.14
strings “ ” or ` `
bool true false
+ - * / %
&& || ! == != >= <=
Loops
for i := 0; i < count; i++ {
}
for i, value := range array {
}
for i <= 10 {
i++
}
Conditions
if i > 10 {
} else if i > 5 {
} else {
}
switch grade {
case 75: fmt.Println("A")
default: fmt.Println("nothing")
}
Arrays
var myArray[5] int
myArray := [5]int {1,2,3,4,5}
mySlice := []int {1,2,3,4,5} //Slice has no size declaration
mySlice2 := mySlice[3:5]
slice := make([]int, 5, 10)
slice = append(slice,0,1)
Maps
Just like dictionaries in python
grades := make(map[string] int)
grades[“John”] = 80
delete(grades,”John”)
Functions
func myFunc(number int) int {
return number + 5
}
func myFunc(number int) (int,int) {
return number + 5, number +6
}
Executes after the enclosing function
defer myFunc()
Undefined number of variables
func uParams(args ...int) int {
}
func divide(num1 int, num2 int) int {
defer func() {
fmt.Println(recover())
}()
answer := num1/ num2
return answer
}
func divide() {
defer func() {
fmt.Println(recover())
}()
panic(“sending to recover”)
}
Functions - defer() and panic()
Closure
Declaring a function inside another
func main() {
myfunc := func() int {}
myfunc()
}
Pointers
x := 8
changeX(&x)
func changeX(x *int){
*x = 10
}
myPointer := new(int)
X
memory address
(0xc0820022b0)
Structures
Go is not object oriented
type Circle struct {
var radius float64
var name string
}
myCircle = Circle{name:”circle1” , radius:5}
func (circle Circle) area() float64 {
return circle.radius*circle.radius*3.14
}
Handling Concurrency
GORoutines
Not expensive as threads
Multiple Goroutines without cost
Channels
GORoutines reads and writes values from an to
channels to communicate
https://golang.org/pkg/sync/atomic/
GO http
Route
Handles the requests and determines which function should
handle that request
Handler
The function that executes when a request is made
Server
The networking code which handles the requests and routes
(Serve mux) multiplexer, http request router
Simple http server
Simple respond writer
package main
import "net/http"
func main() {
http.HandleFunc("/", homeHandler)
http.ListenAndServe(":8100", nil)
}
func homeHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("අ◌ායුෙබ◌ා◌් වන්"))
}
Gorilla Toolkit
Gorilla Mux
$ go get github.com/gorilla/mux
simple buffer writer
Gorilla sessions
$ go get github.com/gorilla/sessions
GO Frameworks Toolkits and Micro Frameworks
https://github.com/golang/go/wiki/LearnServerProgramming
Who uses Go
https://github.com/golang/go/wiki/GoUsers
Thank You
Monthly meetup November 2015 NSBM
Raveen Perera

Weitere ähnliche Inhalte

Was ist angesagt?

Happy Go Programming Part 1
Happy Go Programming Part 1Happy Go Programming Part 1
Happy Go Programming Part 1
Lin Yo-An
 
To Infinity & Beyond: Protocols & sequences in Node - Part 1
To Infinity & Beyond: Protocols & sequences in Node - Part 1To Infinity & Beyond: Protocols & sequences in Node - Part 1
To Infinity & Beyond: Protocols & sequences in Node - Part 1
Bahul Neel Upadhyaya
 

Was ist angesagt? (20)

Go concurrency
Go concurrencyGo concurrency
Go concurrency
 
Golang design4concurrency
Golang design4concurrencyGolang design4concurrency
Golang design4concurrency
 
Happy Go Programming Part 1
Happy Go Programming Part 1Happy Go Programming Part 1
Happy Go Programming Part 1
 
Concurrency with Go
Concurrency with GoConcurrency with Go
Concurrency with Go
 
Implementing Software Machines in Go and C
Implementing Software Machines in Go and CImplementing Software Machines in Go and C
Implementing Software Machines in Go and C
 
Let's golang
Let's golangLet's golang
Let's golang
 
Go Language Hands-on Workshop Material
Go Language Hands-on Workshop MaterialGo Language Hands-on Workshop Material
Go Language Hands-on Workshop Material
 
Clojure+ClojureScript Webapps
Clojure+ClojureScript WebappsClojure+ClojureScript Webapps
Clojure+ClojureScript Webapps
 
Defer, Panic, Recover
Defer, Panic, RecoverDefer, Panic, Recover
Defer, Panic, Recover
 
Introduction to go language programming
Introduction to go language programmingIntroduction to go language programming
Introduction to go language programming
 
Hands on Session on Python
Hands on Session on PythonHands on Session on Python
Hands on Session on Python
 
To Infinity & Beyond: Protocols & sequences in Node - Part 1
To Infinity & Beyond: Protocols & sequences in Node - Part 1To Infinity & Beyond: Protocols & sequences in Node - Part 1
To Infinity & Beyond: Protocols & sequences in Node - Part 1
 
tokyotalk
tokyotalktokyotalk
tokyotalk
 
Reversing the dropbox client on windows
Reversing the dropbox client on windowsReversing the dropbox client on windows
Reversing the dropbox client on windows
 
Python with a SWIG of c++
Python with a  SWIG of c++Python with a  SWIG of c++
Python with a SWIG of c++
 
GopherCon Denver LT 2018
GopherCon Denver LT 2018GopherCon Denver LT 2018
GopherCon Denver LT 2018
 
Free Monads Getting Started
Free Monads Getting StartedFree Monads Getting Started
Free Monads Getting Started
 
Music as data
Music as dataMusic as data
Music as data
 
Script
ScriptScript
Script
 
Faster Python, FOSDEM
Faster Python, FOSDEMFaster Python, FOSDEM
Faster Python, FOSDEM
 

Andere mochten auch (6)

Math1003 1.12 - Binary Addition
Math1003 1.12 - Binary AdditionMath1003 1.12 - Binary Addition
Math1003 1.12 - Binary Addition
 
Binary addition
Binary additionBinary addition
Binary addition
 
Binary arithmetic
Binary arithmeticBinary arithmetic
Binary arithmetic
 
binary arithmetic rules
binary arithmetic rulesbinary arithmetic rules
binary arithmetic rules
 
Binary Arithmetic
Binary ArithmeticBinary Arithmetic
Binary Arithmetic
 
WTF - Why the Future Is Up to Us - pptx version
WTF - Why the Future Is Up to Us - pptx versionWTF - Why the Future Is Up to Us - pptx version
WTF - Why the Future Is Up to Us - pptx version
 

Ähnlich wie Coding in GO - GDG SL - NSBM

Ähnlich wie Coding in GO - GDG SL - NSBM (20)

Golang basics for Java developers - Part 1
Golang basics for Java developers - Part 1Golang basics for Java developers - Part 1
Golang basics for Java developers - Part 1
 
Trivadis TechEvent 2016 Go - The Cloud Programming Language by Andija Sisko
Trivadis TechEvent 2016 Go - The Cloud Programming Language by Andija SiskoTrivadis TechEvent 2016 Go - The Cloud Programming Language by Andija Sisko
Trivadis TechEvent 2016 Go - The Cloud Programming Language by Andija Sisko
 
The GO Language : From Beginners to Gophers
The GO Language : From Beginners to GophersThe GO Language : From Beginners to Gophers
The GO Language : From Beginners to Gophers
 
Golang
GolangGolang
Golang
 
golang_getting_started.pptx
golang_getting_started.pptxgolang_getting_started.pptx
golang_getting_started.pptx
 
Go for Rubyists
Go for RubyistsGo for Rubyists
Go for Rubyists
 
Let's Go-lang
Let's Go-langLet's Go-lang
Let's Go-lang
 
Python ppt
Python pptPython ppt
Python ppt
 
Go 1.10 Release Party - PDX Go
Go 1.10 Release Party - PDX GoGo 1.10 Release Party - PDX Go
Go 1.10 Release Party - PDX Go
 
Introduction to Python Programming | InsideAIML
Introduction to Python Programming | InsideAIMLIntroduction to Python Programming | InsideAIML
Introduction to Python Programming | InsideAIML
 
Introduction to Programming in Go
Introduction to Programming in GoIntroduction to Programming in Go
Introduction to Programming in Go
 
Golang workshop
Golang workshopGolang workshop
Golang workshop
 
Iron Languages - NYC CodeCamp 2/19/2011
Iron Languages - NYC CodeCamp 2/19/2011Iron Languages - NYC CodeCamp 2/19/2011
Iron Languages - NYC CodeCamp 2/19/2011
 
Inroduction to golang
Inroduction to golangInroduction to golang
Inroduction to golang
 
Golang勉強会
Golang勉強会Golang勉強会
Golang勉強会
 
Golang iran - tutorial go programming language - Preliminary
Golang iran - tutorial  go programming language - PreliminaryGolang iran - tutorial  go programming language - Preliminary
Golang iran - tutorial go programming language - Preliminary
 
Introduction to Python Programming
Introduction to Python ProgrammingIntroduction to Python Programming
Introduction to Python Programming
 
Introduction to F#
Introduction to F#Introduction to F#
Introduction to F#
 
Go programming introduction
Go programming introductionGo programming introduction
Go programming introduction
 
Groovy Introduction - JAX Germany - 2008
Groovy Introduction - JAX Germany - 2008Groovy Introduction - JAX Germany - 2008
Groovy Introduction - JAX Germany - 2008
 

Kürzlich hochgeladen

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
SanaAli374401
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
MateoGardella
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 

Kürzlich hochgeladen (20)

Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 

Coding in GO - GDG SL - NSBM

  • 1. coding in GO Monthly meetup November 2015 NSBM Raveen Perera
  • 2. History Created by Robert Griesemer, Rob Pike, Ken Thompson Developed in 2007 and first stable open source release 2009 (BSD) GO Fast, compiled language, directly to machine code and spearheaded by
  • 3. What’s so special about GO ? Compilation Very fast compilation (seconds) No VM needed GOs Assembler Tools go fmt go vet go test go doc Concurrency Asynchronous processes called GOroutines Channels used to pass data between routines Standard Library net/http flag encoding/json encoding/xml Simplicity in Syntax GO stands between C and Python Highly influenced by many other popular programming languages Deployment Can be compiled to a single binary file You can build and compile in your host or server
  • 5. Download and install GO https://golang.org/dl/ Download and install Sublime Text 3 http://www.sublimetext.com/3 and install GOSublime plugin https://github.com/DisposaBoy/GoSublime
  • 6. package main import ( "fmt" ) func main() { fmt.Println("Hello World!") } $ go run helloworld.go
  • 7. The Basics https://golang.org/ref/spec Variables var age int = 40 name := “John Doe” const pi float64 = 3.14 strings “ ” or ` ` bool true false + - * / % && || ! == != >= <=
  • 8. Loops for i := 0; i < count; i++ { } for i, value := range array { } for i <= 10 { i++ }
  • 9. Conditions if i > 10 { } else if i > 5 { } else { } switch grade { case 75: fmt.Println("A") default: fmt.Println("nothing") }
  • 10. Arrays var myArray[5] int myArray := [5]int {1,2,3,4,5} mySlice := []int {1,2,3,4,5} //Slice has no size declaration mySlice2 := mySlice[3:5] slice := make([]int, 5, 10) slice = append(slice,0,1)
  • 11. Maps Just like dictionaries in python grades := make(map[string] int) grades[“John”] = 80 delete(grades,”John”)
  • 12. Functions func myFunc(number int) int { return number + 5 } func myFunc(number int) (int,int) { return number + 5, number +6 } Executes after the enclosing function defer myFunc() Undefined number of variables func uParams(args ...int) int { }
  • 13. func divide(num1 int, num2 int) int { defer func() { fmt.Println(recover()) }() answer := num1/ num2 return answer } func divide() { defer func() { fmt.Println(recover()) }() panic(“sending to recover”) } Functions - defer() and panic()
  • 14. Closure Declaring a function inside another func main() { myfunc := func() int {} myfunc() }
  • 15. Pointers x := 8 changeX(&x) func changeX(x *int){ *x = 10 } myPointer := new(int) X memory address (0xc0820022b0)
  • 16. Structures Go is not object oriented type Circle struct { var radius float64 var name string } myCircle = Circle{name:”circle1” , radius:5} func (circle Circle) area() float64 { return circle.radius*circle.radius*3.14 }
  • 17. Handling Concurrency GORoutines Not expensive as threads Multiple Goroutines without cost Channels GORoutines reads and writes values from an to channels to communicate https://golang.org/pkg/sync/atomic/
  • 18. GO http Route Handles the requests and determines which function should handle that request Handler The function that executes when a request is made Server The networking code which handles the requests and routes (Serve mux) multiplexer, http request router
  • 19. Simple http server Simple respond writer package main import "net/http" func main() { http.HandleFunc("/", homeHandler) http.ListenAndServe(":8100", nil) } func homeHandler(w http.ResponseWriter, r *http.Request) { w.Write([]byte("අ◌ායුෙබ◌ා◌් වන්")) }
  • 20. Gorilla Toolkit Gorilla Mux $ go get github.com/gorilla/mux simple buffer writer Gorilla sessions $ go get github.com/gorilla/sessions
  • 21. GO Frameworks Toolkits and Micro Frameworks https://github.com/golang/go/wiki/LearnServerProgramming
  • 23. Thank You Monthly meetup November 2015 NSBM Raveen Perera