An Introduction to Go

28
An Introduction to Go Imesh Gunaratne Product Lead, WSO2 Private PaaS Committer & PMC Member, Apache Stratos

Transcript of An Introduction to Go

Page 1: An Introduction to Go

An Introduction to Go

Imesh Gunaratne

Product Lead, WSO2 Private PaaSCommitter & PMC Member, Apache Stratos

Page 2: An Introduction to Go

Why Google started Go?

● No major systems language has emerged in over a decade

● Computers are enormously quicker but software development is not faster

● C tradition header file based dependency management is much cleaner and faster in compilation

● Dynamically typed languages such as Python, JS are much easier to use

● Fundamental concepts such as garbage collection & parallel computation were not supported well in popular system languages

https://golang.org/doc/faq

Page 3: An Introduction to Go

Why Google started Go?

● None of the system languages were providing following three aspects in one:○ Efficient compilation○ Efficient execution○ Ease of programming

● Go is an attempt to combine the ease of programming of an interpreted, dynamically typed language with the efficiency and safety of a statically typed, compiled language.

https://golang.org/doc/faq

Page 4: An Introduction to Go

Go ancestors

● Mostly in the C family (basic syntax)● Input from Python, JS ● Significant input from Pascal/Modula/Oberon family

(declarations, packages)● Ideas from CSP style languages Newsqueak, Limbo

(concurrency)

In every respect it’s a new language built by thinking what programmers do and how to make programming much efficient and more fun!

https://golang.org/doc/faq

Page 5: An Introduction to Go

Go = C + Python + JSIt’s more like C with Python/JS style syntax

Page 6: An Introduction to Go

What Go provides?

● A systems programming language● Compiled, directly compile into binary like C● Binary is platform specific● Statically typed, but support dynamic declarations● No virtual machine like in Java● No exception handling, uses multi-value returns ● No classes, use structs with functions● Not object oriented, but has support for interfaces, more

like functional programming● CSP-style concurrent programming● Provides garbage collectionhttps://en.wikipedia.org/wiki/Go_(programming_language)

Page 7: An Introduction to Go

Advantages of Go

● Fast compilation, a large program compiles in seconds● Type system has no hierarchy, so no time is spent

defining the relationships between types● Improved dependency analysis compared to C-style

include files● Fully garbage collected● Fundamental support for concurrent execution &

communication● Designed to implement system software to be run on

multi-core machines

https://golang.org/doc/faq

Page 8: An Introduction to Go

Say Hello World in GoConstructs & Syntax

Page 9: An Introduction to Go

Hello world

https://gobyexample.com/hello-world

Page 10: An Introduction to Go

Standard ConstructsMost common constructs used in other languages

Page 11: An Introduction to Go

Variables

https://gobyexample.com/variables

Page 12: An Introduction to Go

Values

https://gobyexample.com/values

Page 13: An Introduction to Go

Functions

https://gobyexample.com/functions

Page 14: An Introduction to Go

If/Else

https://gobyexample.com/if-else

Page 15: An Introduction to Go

For

https://gobyexample.com/for

Page 16: An Introduction to Go

Switch

https://gobyexample.com/switch

Page 17: An Introduction to Go

Map

https://gobyexample.com/map

Page 18: An Introduction to Go

Variadic functions

https://gobyexample.com/variadic-functions

Page 19: An Introduction to Go

Pointers

https://gobyexample.com/pointers

Page 20: An Introduction to Go

Structs

https://gobyexample.com/structs

Page 21: An Introduction to Go

Go Specific ConstructsConstructs introduced in Go

Page 22: An Introduction to Go

Multiple return values

https://gobyexample.com/multiple-return-values

Page 23: An Introduction to Go

Closures

https://gobyexample.com/closures

Page 24: An Introduction to Go

Range (for each)

http://stackoverflow.com/questions/7782411/is-there-a-foreach-in-go

Page 25: An Introduction to Go

Slices

https://gobyexample.com/slices

Page 26: An Introduction to Go

Goroutines

https://gobyexample.com/goroutines

Page 27: An Introduction to Go

Channels

https://gobyexample.com/channels

Page 28: An Introduction to Go

References● Golang FAQ:

https://golang.org/doc/faq

● Go, Wikipedia:https://en.wikipedia.org/wiki/Go_(programming_language)

● Communicating Sequential Processes (CSP), Wikipedia:https://en.wikipedia.org/wiki/Communicating_sequential_processes

● Static typing, Wikipedia:https://en.wikipedia.org/wiki/Type_system#Static_typing

● Dynamic programming language, Wikipedia:https://en.wikipedia.org/wiki/Dynamic_programming_language

● Variadic function, Wikipedia: https://en.wikipedia.org/wiki/Variadic_function

● Closure, Wikipedia:https://en.wikipedia.org/wiki/Closure_(computer_programming)