An Introduction to Go

Post on 12-Apr-2017

430 views 0 download

Transcript of An Introduction to Go

An Introduction to Go

Imesh Gunaratne

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

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

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

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

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

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)

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

Say Hello World in GoConstructs & Syntax

Hello world

https://gobyexample.com/hello-world

Standard ConstructsMost common constructs used in other languages

Variables

https://gobyexample.com/variables

Values

https://gobyexample.com/values

Functions

https://gobyexample.com/functions

If/Else

https://gobyexample.com/if-else

For

https://gobyexample.com/for

Switch

https://gobyexample.com/switch

Map

https://gobyexample.com/map

Variadic functions

https://gobyexample.com/variadic-functions

Pointers

https://gobyexample.com/pointers

Structs

https://gobyexample.com/structs

Go Specific ConstructsConstructs introduced in Go

Multiple return values

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

Closures

https://gobyexample.com/closures

Range (for each)

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

Slices

https://gobyexample.com/slices

Goroutines

https://gobyexample.com/goroutines

Channels

https://gobyexample.com/channels

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)