Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

of

The GO Language : From Beginners to Gophers Slide 1 The GO Language : From Beginners to Gophers Slide 2 The GO Language : From Beginners to Gophers Slide 3 The GO Language : From Beginners to Gophers Slide 4 The GO Language : From Beginners to Gophers Slide 5 The GO Language : From Beginners to Gophers Slide 6 The GO Language : From Beginners to Gophers Slide 7 The GO Language : From Beginners to Gophers Slide 8 The GO Language : From Beginners to Gophers Slide 9 The GO Language : From Beginners to Gophers Slide 10 The GO Language : From Beginners to Gophers Slide 11 The GO Language : From Beginners to Gophers Slide 12 The GO Language : From Beginners to Gophers Slide 13 The GO Language : From Beginners to Gophers Slide 14 The GO Language : From Beginners to Gophers Slide 15 The GO Language : From Beginners to Gophers Slide 16 The GO Language : From Beginners to Gophers Slide 17 The GO Language : From Beginners to Gophers Slide 18 The GO Language : From Beginners to Gophers Slide 19 The GO Language : From Beginners to Gophers Slide 20 The GO Language : From Beginners to Gophers Slide 21 The GO Language : From Beginners to Gophers Slide 22 The GO Language : From Beginners to Gophers Slide 23 The GO Language : From Beginners to Gophers Slide 24 The GO Language : From Beginners to Gophers Slide 25 The GO Language : From Beginners to Gophers Slide 26 The GO Language : From Beginners to Gophers Slide 27 The GO Language : From Beginners to Gophers Slide 28 The GO Language : From Beginners to Gophers Slide 29 The GO Language : From Beginners to Gophers Slide 30 The GO Language : From Beginners to Gophers Slide 31 The GO Language : From Beginners to Gophers Slide 32 The GO Language : From Beginners to Gophers Slide 33 The GO Language : From Beginners to Gophers Slide 34 The GO Language : From Beginners to Gophers Slide 35 The GO Language : From Beginners to Gophers Slide 36 The GO Language : From Beginners to Gophers Slide 37 The GO Language : From Beginners to Gophers Slide 38 The GO Language : From Beginners to Gophers Slide 39 The GO Language : From Beginners to Gophers Slide 40 The GO Language : From Beginners to Gophers Slide 41 The GO Language : From Beginners to Gophers Slide 42 The GO Language : From Beginners to Gophers Slide 43 The GO Language : From Beginners to Gophers Slide 44 The GO Language : From Beginners to Gophers Slide 45 The GO Language : From Beginners to Gophers Slide 46 The GO Language : From Beginners to Gophers Slide 47 The GO Language : From Beginners to Gophers Slide 48 The GO Language : From Beginners to Gophers Slide 49 The GO Language : From Beginners to Gophers Slide 50 The GO Language : From Beginners to Gophers Slide 51 The GO Language : From Beginners to Gophers Slide 52 The GO Language : From Beginners to Gophers Slide 53 The GO Language : From Beginners to Gophers Slide 54 The GO Language : From Beginners to Gophers Slide 55
Upcoming SlideShare
What to Upload to SlideShare
Next
Download to read offline and view in fullscreen.

0 Likes

Share

Download to read offline

The GO Language : From Beginners to Gophers

Download to read offline

These are the slides for the seminar to have a basic overview on the GO Language, By Alessandro Sanino.
They were used on a Lesson in University of Turin (Computer Science Department) 11-06-2018

Related Books

Free with a 30 day trial from Scribd

See all

Related Audiobooks

Free with a 30 day trial from Scribd

See all
  • Be the first to like this

The GO Language : From Beginners to Gophers

  1. 1. THE GO PROGRAMMING LANGUAGE FROM BEGINNERS TO GOPHERS ALESSANDRO SANINO UNIVERSITY OF TURIN COMPUTER SCIENCE DEPARTMENT
  2. 2. AGENDA  Introduction  A bit of context  Go tour  Go VS everybody  Installing Go  Testing and Benchmarking  Coding examples  Conclusions and Q&A
  3. 3. A BIT OF CONTEXT GOLANG HISTORY AND USAGE MACROAREAS
  4. 4. LANGUAGE HISTORY  The language was announced in November 2009  First official release in March 2012 (v. 1.0.0)  Inherit characteristics from procedural languages  Supporting new paradigms  Backward C code compatibility  Optimization for concurrency and network efficiency
  5. 5. IDEAS  No more boilerplate (ex not as Angular / React / Laravel / etc…)  Easy and efficient concurrency handling (not like js)  Amazing speed (up to 10x faster than node programs which runs the same algorithm)  Fast to compile (not like npm or composer scripts) ► Slow Reflection Mechanism (improving) ► Huge executable file (result of compilation > 1 MB quite often)
  6. 6. GO TOUR LANGUAGE SYNTAX AND FEATURES
  7. 7. GO RULES-OF-THUMB ► Semantic syntax ► Multi-Paradigm, Functional/Procedural Hybrid Style ► Static Types ► Easy access to packages, along with source code (go get) ► Type inference before compiling ► Reflection (basic) ► First letter Uppercase = Public, otherwise Private to the package it belongs to ► static and dynamic alloc, similarly to C language (* and &) ► Attach functions to structs (methods) and implement interfaces transparently
  8. 8. SEMANTIC SYNTAX
  9. 9. ASSIGNING VALUES
  10. 10. NATIVE TYPES  int (int8 int16 int32 int64 uint8 uint16 uint32 uint64)  float (float32)  double (float64)  string  map[type1]type2 (ex. map[string]int)  interface{}  slice types and array types (ex int[], float32[])  Error interface (error)  Structs (struct)  Generic Interfaces (interface)
  11. 11. ARRAY AND SLICE TYPES  The array type represents a fixed-size array implementation it is created as arr := [N]int { /* inline definition */ } //or var arr2 [N]int arr2[index] = 5 The array type cannot change its length, if this is necessary slices comes into play.  The slice type represents a dinamic size array implementation It is created as slc := arr[1:4] //from arrays or other slices //via dinamic alloc slc2 := make([]int, allocatedCells, capacity)
  12. 12. ADDITIONAL NOTES  len(arrayOrSliceOrMap) gives the length of the given array or slice or map  Arrays, Slices and Map can be iterated with a for loop  To add elements to slices, use append(slice, items…) It does not work with arrays!!!
  13. 13. ADDITIONAL NOTES
  14. 14. MAP AND INTERFACE TYPES  The map type represents a dictionary data structure implementation it is created as myMap := make(map[keyType]valueType, capacity)  The interface{} type is a generic type (similar to the object type in C# or the void* type in C)
  15. 15. MISCELLANEOUS EXAMPLES
  16. 16. THE ERROR INTERFACE  A simple string abstraction in its simplest form (stringError)  Error is an interface, so it can be implemented
  17. 17. PANIC AND RECOVER  Handles errors the similar way to the try-throw-catch mechanism of other languages
  18. 18. NEVER PANIC UNLESS NECESSARY ► panic is unsafe ► error is better ► few exceptions
  19. 19. DEFER  Followed by a function call, tells the runtime to execute it only at the end of the lifecycle of the caller function (used to close files, connections, recover)
  20. 20. GO By using go «functionCall» we are able to create a goroutine which will execute concurrently the specified function call. An example of usage is: go func() { // do something concurrently }() // or func DoSomethingElse() { fmt.Println(os.args) } go DoSomethingElse() // does something else concurrently
  21. 21. GO Goroutines are one of the cases where PANIC - RECOVER is useful
  22. 22. CHANNELS  Used for communication between goroutines  Support atomic insertion and extraction  Can be blocking or non blocking DO NOT COMMUNICATE BY SHARING MEMORY, INSTEAD SHARE MEMORY BY COMMUNICATING
  23. 23. CHANNEL EXAMPLE
  24. 24. GOLANG DOCS  godoc tool creates documentation from intestation comments in packages and functions  https://golang.org website contains all info regarding all non discussed details (ex. Packages, Channels, Goroutines)  https://godoc.org website contains all useful info regarding standard libraries (documentation + examples + how-to-document)
  25. 25. GO VS EVERYBODY COMPARISON BETWEEN GO AND OTHER TECHNOLOGIES
  26. 26. MODERN ALTERNATIVES TO GO  Node.js  PHP (from version 7)  Python3  Ruby  Java  .NET  etc…
  27. 27. GO PERFORMANCE EXAMPLE Task : execute 2000 parallel ops in a web server calling an endpoint Result shown: average response time N : number of request considered to calculate avg time Source: https://goo.gl/NarBMV
  28. 28. GO VS NODE  Go is a lot faster than nodejs, expecially on concurrent and scalable applications (nodejs is single threaded, Go can handle millions of concurrent goroutines on different threads)
  29. 29. GO VS NODE (CONTINUES…)  Go is still a niche language and lacks of a lot of tools javascript has to debug, handle errors, etc…  Nodejs code is harder to maintain, due to the nature of the language (callback hell, promise hell, etc…)
  30. 30. INSTALLING GO HOW TO INSTALL GO TOOLS ON YOUR PC
  31. 31. WINDOWS  Download the executable from golang.org website  Install it  The engine will be available in C:/Go
  32. 32. LINUX (Ubuntu based)  Follow this guide https://github.com/golang/go/wiki/Ubuntu  Alternatively, run this and setup environment variables (see next slide)  Download tar.gz from golang.org for your distro, then tar –C /usr/local –xzf go$VERSION.$OS-$ARCH.tar.gz For example tar –C /usr/local –xzf go1.2.1.linux-amd64.tar.gz  Add your go tools to your $PATH variable export PATH=$PATH:/usr/local/go/bin  Verify other GOENV variables are properly set (next slide)
  33. 33. GO ENVIRONMENT VARIABLES  GOROOT : The root directory where go engine is installed (/usr/local/go)  GOPATH : The root of your workspace (Go needs it, go files outside won’t be compiled, go get puts packages in there too) $HOME/go on Linux or %USERPROFILE%/go on windows  GOBIN : The root of the folder containing Go binaries (/usr/local/go/bin)  GOOS and GOARCH : Respectively the OS and the ARCH the go compiler will compile for
  34. 34. TESTING AND BENCHMARKING USING GO TOOLS FOR EASY TESTS AND BENCHMARKS
  35. 35. go test  Simple integrated tool to test .go files  By following simple rules it is easy to create tests  Tests can also be served as examples and be included in documentation
  36. 36. go test  go test (simple as that)  func TestOtherFunction(t *testing.T) tests OtherFunction behaviour  func ExampleOtherFunction() creates a testable example for the function, which will be included in docs if passes  testing.T contains everything needed to signal pass or errors in tests  Create a file with same name of your .go file and add _test to both filename and package name to have a test file
  37. 37. Test Example package math type Int struct { Val int } func (a *Int) Sum(b Int) Int { return Int{ Val : a.Val + b.Val } } package math_test import ( "fmt", "testing", "math" ) func TestSum(t *testing.T) { a := math.int{val : 1} b := math.int{val : 2} if a.Sum(b).val != 3 { t.Error("Something wrong here") } }
  38. 38. Testable Example (stringutil_test.go) package stringutil_test import ( "fmt" ) func ExampleReverse() { fmt.Println(stringutil.Reverse("hello") // Output: olleh }
  39. 39. go benchmarks (go test -bench .) Similarly to Tests we can create benchmarks of our algorithms  func BenchmarkOtherFunction(b *testing.B) benchmarks another function  b.N represents the number of times the benchmark will be run (it depends on CPU load and speed of the execution)  Benchmarks, like tests, can be aborted at any time  Benchmarks can be done also to verify efficiency of concurrent goroutines
  40. 40. go benchmarks (go test -bench .) package sorting_test import( "sorting" "testing" ) func BenchmarkSelectionSort(b *testing.B) { array := initializeBenchIntArray() for n := 0; n < b.N; n++ { sorted := sorting.SelectionSort(array) if !sorting.isSorted(sorted) { b.Error("Not sorted") } } }
  41. 41. BREAK TIME
  42. 42. HANDS DIRTY IN CODE SOME GUIDED GO EXERCISES
  43. 43. fibonacci.go Create a program that prints the Fibonacci sequence up to a certain N we pass as parameter $> ./fibo 5 $> 0 1 1 2 3 $> ./fibo $> missing N $> ./fibo -1 $> error $> ./fibo invalid $> error
  44. 44. WORK TIME
  45. 45. Producer - Consumer Problem in GO Create a simulation of the Producer - Consumer Problem in GO $> ./prodCons $> Produced item 81 by Producer 0 $> Produced item 887 by Producer 1 $> Consumed item 81 by Consumer 1 $> Produced item 847 by Producer 0 $> Produced item 59 by Producer 1 $> ….
  46. 46. WORK TIME
  47. 47. Writing an API in Go  There are a lot of useful usable framework, and they vary in efficiency and readibility  labstack/echo is the minimal engine, with a speed up to 10 times faster than net/http package  goadesign/goa is a more complex engine, design oriented, to develop complex API  Today we will use labstack/echo !!!
  48. 48. Writing an API in Go Create an api to get quotes and show them to users $> ./server & $> ./client gimmeRandomQuote $> One apple per day keeps the doctor away
  49. 49. Writing an API in Go We need:  Golang server (labstack/echo) to handle the backend  Golang Client to send requests to the server as frontend
  50. 50. WORK TIME
  51. 51. CONCLUSIONS WHAT DO WE LEARNT SO FAR
  52. 52. We learnt to  Write go code following examples and guidelines  Test code and benchmark performance  Write an API to serve content to customers
  53. 53. REFERENCES  GO by example: https://gobyexample.com  Go comparison with other languages (Toptal) : https://goo.gl/NarBMV  Go website : https://golang.org  Godoc website : https://godoc.org  Godoc guidelines : https://goo.gl/iG5X28  Go books : https://goo.gl/hXfZyC
  54. 54. ABOUT THE AUTHOR https://linkedin.com/in/alessandrosanino https://github.com/saniales https://github.com/thebotguys https://github.com/crypto-crew-tech Alessandro Sanino Bandito – Bra (CN), Italy Currently Blockchain Researcher @ unito
  55. 55. “ ” Go is not meant to innovate programming theory. It’s meant to innovate programming practice ~ Samuel Tesla, Article Writer and Programmer

These are the slides for the seminar to have a basic overview on the GO Language, By Alessandro Sanino. They were used on a Lesson in University of Turin (Computer Science Department) 11-06-2018

Views

Total views

357

On Slideshare

0

From embeds

0

Number of embeds

2

Actions

Downloads

10

Shares

0

Comments

0

Likes

0

×