Go lang

35
Suelen Goularte Carvalho [email protected] Computação Paralela e Distribuída Profº Alfredo Goldman Junho/2015

Transcript of Go lang

Page 1: Go lang

Suelen Goularte [email protected]

Computação Paralela e DistribuídaProfº Alfredo Goldman

Junho/2015

Page 2: Go lang

Go is a programming language designed by Google to help solve Google's problems.

What is Go?

And has big problems!

Page 3: Go lang

Which (big) problems?

● Hardware is big and the software is big● There are many millions of lines of software● Servers mostly in C++ and lots of Java and Python● Thousands of engineers work on the code● And of course, all this software runs on zillions of

machines.

Page 4: Go lang

In short, development at is big, can be slow, and is often

clumsy. But it is effective.

''''

https://talks.golang.org/2012/splash.article

Page 5: Go lang

A lot of others people help to bring go from prototype to reality.

Go became a public Open Source project.

https://golang.org/doc/faq#history

2008 2009 20102007

Starts to have adoption by other programmers

Started and built by Robert Griesemer, Rob Pike and Ken Thompson as a part-time project.

History

Page 6: Go lang

● Ken Thompson (B, C, Unix, UTF-8) ● Rob Pike (Unix, UTF-8) ● Robert Griesemer (Hotspot, JVM) ...and a few others engineers at Google

Who were the founders?

Page 8: Go lang

● Eliminate slowness● Eliminate clumsiness● Improve productive ● Maintain (and improve) scale

It was designed by and for people who write, read, debug and maintain large software systems.

Go's purpose is not to do research programming language design.

Go's purpose is to make its designers' programming lives better.

Why Go?

Page 9: Go lang

Go is a compiled, concurrent, garbage-collected, statically typed language developed at .

What is Go?

Page 10: Go lang

fix, fmt, get, install, list, tool, version, vet.

build compile packages and dependencies

run compile and run Go program

clean remove object files

env print Go environment information

test test packages and benchmarks

Go is a tool for managing Go source code...

Mainly tools:

Others tools:

Page 11: Go lang

Who are using today?

https://github.com/golang/go/wiki/GoUsers

Page 12: Go lang

❏ Compiled❏ Garbage-collected❏ Has your own runtime❏ Simple syntax❏ Great standard library❏ Cross-platform❏ Object Oriented (without inheritance)❏ Statically and stronger typed❏ Concurrent (goroutines)❏ Closures❏ Explicity dependencies❏ Multiple return values❏ Pointers❏ and so on...

What will you see in Go?

Page 13: Go lang

Have not been implemented in favor of efficiency.

❏ Exception handling❏ Inheritance❏ Generics❏ Assert❏ Method overload

What will you not see in Go?

Page 14: Go lang

see a bit of code!

Page 15: Go lang

Packages

● Each Go program are compound per packages● Programs starts from main package● This example are using the ftm and math packages

$ go run packages.goMy favorite number is 1

Page 16: Go lang

● The var instruction declares a list of variables● The type is informed at the end● The var instruction could be in a package or in a function● The var instruction could includes initializers, 1 per variable. In

this case, the type could be ommited because it will be inferred

Variables

$ go run variables.go0 false false false

$ go run variables-with-initiali1 2 true false no!

Page 17: Go lang

$ go run constants.goHello world! Happy 3.14 Day! Go rules? true

● Constants are declared like variables but with keyword const● Can not use the syntx :=

Constants

Page 18: Go lang

● Inside a function, the short attribution instruction := can be used instead of a var declaration

Short variables declarations

$ go run short-variable-declarations.go1 2 3 true false no!

Page 19: Go lang

Functions● Functions could have zero or more arguments● Notice that the type comes after the parameter name,

like variables

$ go run functions.go55

Page 20: Go lang

● A function can have multiple return values

Multiple return values

$ go run multiple-results.goworld hello

Page 21: Go lang

● Go has just for as looping structure● It is very similar with C or Java code, except for ( )● Start and end declarations can be empty

Looping For

$ go run for.go45

$ go run for-continu1024

Page 22: Go lang

$ go run for-is-go-while.go1024

● Semicolon can be removed and you will have while ● for can run forever

Looping "while" and forever

$ go run forever.goprocess took too long

Page 23: Go lang

● It is very similar with C or Java code, except for ( )

if Condition

$ go run if.go1.4142135623730951 2i

Page 24: Go lang

$ go run switch.goGo runs on nacl.

● It is very similar with C or Java code, except for ( )

Switch Condition

Page 25: Go lang

$ go run defer.gohello world

Defer● Postponing the execution of a function until the function returns● The arguments of the deferred calls are evaluated immediately

Page 26: Go lang

What more?● Pointer● Struct● Matrix● Slice● Range● Map● Value function● Closures● Method● Interface● Stringer● Error● and a lot of more!!!

http://go-tour-br.appspot.com

Page 27: Go lang

$ go run http.go

A web server● It is just simple to build a web server with 15 lines or less!!

Could you belive that???

Page 28: Go lang

● To execute a goroutine, just go!

● To send or receive information between the goroutines, use channels

● Use the GOMAXPROCS environment variable to define the amount of threads

Concurrency (goroutines)

Page 29: Go lang

$ go run goroutines.gohelloworldhelloworldhelloworldhelloworldhello

● A goroutine is a lightweight thread managed by Go runtime

Goroutines

Page 30: Go lang

$ go run channels.go17 -5 12

Channels● Channels are typed's conduit through which you can send and receive

values with the channel operator <-

Page 31: Go lang

Unbuffered Channels

http://www.goinggo.net/2014/02/the-nature-of-channels-in-go.html

c := make (chan int)

Page 32: Go lang

Buffered Channels

http://www.goinggo.net/2014/02/the-nature-of-channels-in-go.html

c := make (chan int, 10)

Page 33: Go lang

Now you are ready to

!

Page 34: Go lang

Bibliografia❏ http://golang.org❏ http://go-tour-br.appspot.com/❏ https://tour.golang.org❏ http://www.golangbr.org/❏ https://vimeo.com/49718712❏ http://gophercon.com❏ http://www.infoq.com/br/news/2014/09/go-1-3❏ http://www.casadocodigo.com.br/products/livro-google-go❏ https://pt.wikipedia.org/wiki/Inferno_(sistema_operacional)❏ http://www.grokpodcast.com/series/a-linguagem-go/❏ https://pt.wikipedia.org/wiki/Go_(linguagem_de_programação)❏ https://gobyexample.com❏ http://www.goinggo.net/2014/02/the-nature-of-channels-in-go.html❏ http://www.goinggo.net/2013/09/detecting-race-conditions-with-go.html?m=1❏ https://en.wikipedia.org/wiki/Green_threads❏ http://www.toptal.com/go/go-programming-a-step-by-step-introductory-tutorial

Page 35: Go lang

Questions?