go programming language
TRANSCRIPT
SEMINAR
GO PROGRAMMING LANGUAGE
OutlineOutline
• GOLANG history• Features• Disadvantages• Learning Go
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
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
Features• Clean and Simple • Concurrent• Garbage collection• Fast• Safe • Standard format• Open Source• Has methods but not a conventional object-oriented
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
Learning GOLearning GO
• Environment setup• Install golang(ubuntu): sudo apt-get install golang-go• Install Atom(code editor): sudo apt-get install atom
Learning GOLearning GO• References website• https://play.golang.org/• https://tour.golang.org• https://gobyexample.com/
• “Helloworld” program
Learning GOLearning GO
• Types• Variables• Function• Control Structure• Object Oriented• Concurrency
TypeType• Basic Type
– boolean– String– Int, int8, int16, int32, int64; uint, uint8, uint16, uint32,
uint64, uintptr– Byte– Rune– Float32, float64– Complex64, complex128– Constants
TypeType• Composite Type
– Arrays– Slices– maps– Structs– JSONS– Text and HTML template
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)
ConstantsConstants• Constant declaration
• Const x = “hello” • Iota
• Simplify definitions of incrementing number• Const(
Sunday = iota // 0Monday // 1Tuesday // 2Wednesday // 3)
ArraysArrays
• Numbered sequence of elements of a single type• var a [5]int• b:= [5]int{1,2,3,4,5}
SlicesSlices
• Key data type• s:= make([]int,3) • b:= []int{1,2,3,4,5}• b[0:2] // [1 2]
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}
PointersPointers
• Go supports pointer• No pointer arithmetic
i:=1var ip *int = &ifmt.Println(“value”,i)fmt.Println(“pointer”, &i)fmt.Println(“pointer”, ip)
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”)
}
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)
FunctionFunction
• Anomyous function• Ex:
func squares() func() int { var x int return func() int { x++ return x * x } }
FunctionFunction
• Defer• Ex:
func main() { f := createFile("defer.txt") defer closeFile(f) writeFile(f) }
FunctionFunction
• Named return values• Ex:
func split(sum int)(x int) {x = sum * 4 / 9return
}
Object OrientedObject Oriented
• Structs• Method• Interfaces
StructsStructsA collection of fields
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 }
InterfacesInterfaces- named collections of method signatures- no implements keyword
ConcurencyConcurency
• Goroutine• Channel• Select
GoroutineGoroutine
• A lightweight thread of execution• Ex:
ChannelChannel
• Are the pipes that connect concurent goroutines• Ex:
ChannelChannel
• By default, channels are unbuffered• Make buffered channel
SelectSelect
• Let you wait on multiple channel operations
Learning GOLearning GO
• GO Web Framework• Revel• Martini• Gocraft/web• Gorilla• Gogin
ReferencesReferences
http://www.slideshare.net/fmamud/ftd-golanghttp://www.slideshare.net/aitjcize/go-lang-tutorial