go programming language

35
SEMINAR GO PROGRAMMING LANGUAGE

Upload: tung-vu

Post on 06-Apr-2017

136 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: GO programming language

SEMINAR 

GO PROGRAMMING LANGUAGE

Page 2: GO programming language

OutlineOutline

• GOLANG history• Features• Disadvantages• Learning Go

Page 3: GO programming language

Go HistoryGo History

• Developed at Google in year 2007 by Robert Griesemer, Rob Pike, and Ken Thompson.

• Strongly and statically typed, provides inbuilt support for garbage collection and supports concurrent programming.

• Announced in November 2009

Page 4: GO programming language

Go HistoryGo History

• GO used by • Twitter’s new system architecture for mobile has a service

written in Go that handles 5 billions requests per day.• Youtube use Go to manange MYSQL server.• Dropbox has shifted lots of stacks from python to Go

Page 5: GO programming language
Page 6: GO programming language

Features• Clean and Simple • Concurrent• Garbage collection• Fast• Safe • Standard format• Open Source• Has methods but not a conventional object-oriented 

language

Page 7: GO programming language

DisadvantagesDisadvantages

• Still a very young language• Go’s tooling is really weird• It is still not so easy to learn Go and it’s difficult to handle

errors in it

Page 8: GO programming language

Learning GOLearning GO

• Environment setup• Install golang(ubuntu): sudo apt-get install golang-go• Install Atom(code editor): sudo apt-get install atom

Page 9: GO programming language

Learning GOLearning GO• References website• https://play.golang.org/• https://tour.golang.org• https://gobyexample.com/

• “Helloworld” program

Page 10: GO programming language

Learning GOLearning GO

• Types• Variables• Function• Control Structure• Object Oriented• Concurrency

Page 11: GO programming language

TypeType• Basic Type

– boolean– String– Int, int8, int16, int32, int64; uint, uint8, uint16, uint32,

uint64, uintptr– Byte– Rune– Float32, float64– Complex64, complex128– Constants

Page 12: GO programming language

TypeType• Composite Type

– Arrays– Slices– maps– Structs– JSONS– Text and HTML template

Page 13: GO programming language

VariablesVariables• Declare one or more variables

• var x,y,z int• Infer the type of initialized variables

• var x,y,z = true,false,”hello”• Zero-value initialization

• var x int // x = 0• var y bool // y = false

• Short hand• x,y,z:= true,2, “ok”

• Grouping• var( x int = 1

y bool = true)

Page 14: GO programming language

ConstantsConstants• Constant declaration

• Const x = “hello” • Iota

• Simplify definitions of incrementing number• Const(

Sunday = iota // 0Monday // 1Tuesday // 2Wednesday // 3)

Page 15: GO programming language

ArraysArrays

• Numbered sequence of elements of a single type• var a [5]int• b:= [5]int{1,2,3,4,5}

Page 16: GO programming language

SlicesSlices

• Key data type• s:= make([]int,3) • b:= []int{1,2,3,4,5}• b[0:2] // [1 2]

Page 17: GO programming language

MapsMaps

• Built-in associative data type• m:= make(map[string]int)• m[“one”] = 1• m[“nine”] = 9• delete (m,“one”)• n:= map[string]int{“one”: 1, “two”: 2}

Page 18: GO programming language

PointersPointers

• Go supports pointer• No pointer arithmetic

i:=1var ip *int = &ifmt.Println(“value”,i)fmt.Println(“pointer”, &i)fmt.Println(“pointer”, ip)

Page 19: GO programming language

Control structureControl structure

• If• If v := math.Pow(x, n); v < 10 { fmt.Println(v) }

• For• Switch

• switch os := runtime.GOOS; os {case "darwin": fmt.Println("OS X.")default: fmt.Println(“unknown”)

}

Page 20: GO programming language

FunctionFunction

• Declaration• func myFunction(a,b,c int)(int,int){}

• Multiple returned values• x,y := myFunction(2,3,4)• _,y := myFunction(2,3,4)

• Variadic function• func sum(nums ...int){…code here……}• sum(1,2)• sum(1,2,3)

Page 21: GO programming language

FunctionFunction

• Anomyous function• Ex:

func squares() func() int { var x int return func() int { x++ return x * x } }

Page 22: GO programming language

FunctionFunction

• Defer• Ex:

func main() { f := createFile("defer.txt") defer closeFile(f) writeFile(f) }

Page 23: GO programming language

FunctionFunction

• Named return values• Ex:

func split(sum int)(x int) {x = sum * 4 / 9return

}

Page 24: GO programming language

Object OrientedObject Oriented

• Structs• Method• Interfaces

Page 25: GO programming language

StructsStructsA collection of fields

Page 26: GO programming language

MethodMethodBasically look like function

package main import "fmt" type Int int // only type defined in package func (x Int) square() Int { return x * x } func main() { x := Int(5) // type conversion fmt.Printf(x.square()) // Bình phương của 5 là: 25 }

Page 27: GO programming language

InterfacesInterfaces- named collections of method signatures- no implements keyword

Page 28: GO programming language

ConcurencyConcurency

• Goroutine• Channel• Select

Page 29: GO programming language

GoroutineGoroutine

• A lightweight thread of execution• Ex:

Page 30: GO programming language

ChannelChannel

• Are the pipes that connect concurent goroutines• Ex:

Page 31: GO programming language

ChannelChannel

• By default, channels are unbuffered• Make buffered channel

Page 32: GO programming language

SelectSelect

• Let you wait on multiple channel operations

Page 33: GO programming language
Page 34: GO programming language

Learning GOLearning GO

• GO Web Framework• Revel• Martini• Gocraft/web• Gorilla• Gogin

Page 35: GO programming language

ReferencesReferences

http://www.slideshare.net/fmamud/ftd-golanghttp://www.slideshare.net/aitjcize/go-lang-tutorial