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.

Introduction to Go programming


Published on

This presentation covers the inception of Go, its differences with conventional programming languages and some language features.

Published in: Software, Technology

Introduction to Go programming

  2. 2. Agenda Modern Compute Environment Focus: What Problem does it Solve? History How is it Achieved For C Programmers - Major Differences
  3. 3. Challenges in Modern Compute Landscape
  4. 4. Environment Multi-Core Networked Systems Computational Clusters Web Programming Model Thousands of Programmers Same code base Scale of Development
  5. 5. Legacy Languages Language Feature Complex Garbage Collection Missing language support for Concurrency Compilation Times Dependency Management
  6. 6. Programmers digress from the real task! Crux:
  7. 7. Focus
  8. 8. “Go’s purpose is therefore not to do research into programming language design; it is to improve the working environment for its designers and their coworkers.” –Rob Pike (Co-Designer of Go and Distinguished Engineer at Google)
  9. 9. History
  10. 10. History Inception: 2007 Go 1.0 released in 2012 Rob Pike, Ken Thompson and Robert Griesemer
  11. 11. History Binary = 2000 X C++ Source Bytes Unused Library Includes Conditional Includes Extremely Slow Compilation Even on Distributed Compilation System!
  12. 12. Desired Features Light Weight Increase Programmer Efficiency Compiled Language Concurrency Garbage Collection Statically Typed
  13. 13. How is it Achieved?
  14. 14. How No unused import packages Dependency graph is precise No dependency cycles Only exported data available One object both exported and complete data
  15. 15. How 40X faster compilation times than C++!
  16. 16. Differences between Go and C
  17. 17. Syntax Import paths are URLs Cleaner syntax
  18. 18. Syntax //C typedef struct A { int a; char b; } //Go type A struct { a int b char }
  19. 19. Syntax /* C */ //Expression Syntax int main(int argc, char *argv[]) // The Clockwise-Spiral Rule in C syntax parsing int (*(*fp)(int (*)(int, int), int))(int, int) /* Go */ // Type Syntax func main(argc int, argv []string) int //Left to right f func(func(int,int) int, int) func(int, int) int
  20. 20. Syntax //Go can also return multiple value from function func ReadFile(r io.Reader) (num int, err error) { ... } ... n, err := ReadFile(r)
  21. 21. Syntax package util //Counter is visible when package is imported var Counter int //name is not var name string //Seen func ThisIsAlsoAvailable() error { } //not seen func thisIsAPrivateFunction() error { }
  22. 22. Scopes Universe (language identifiers) Package File Function Block
  23. 23. Semantics No pointer arithmetics No implicit numeric conversions Array bounds are always checked types are not type aliases type X int // X and int are distinct Interface types Reflection Type switch
  24. 24. Concurrency Go Routines Channels - Communication Pipes
  25. 25. Concurrency Light-weight threads of execution Managed by Go Runtime Run concurrently with other go routines Example Shared resource manager Querier DB Reader etc... Go Routines
  26. 26. Concurrency package main import “fmt” func myPrinter(str string) { fmt.Println(str) } func main() { myPrinter(“one”) go myPrinter(“maybetwo”) go myPrinter(“maybethree”) } //Output one maybetwo Maybethree //Or one maybethree maybetwo //Or one ...
  27. 27. Concurrency Shared pipes connecting concurrent go routines Channels
  28. 28. Concurrency package main import "fmt” func main() { messages := make(chan string) // Send a value `channel <-` go func() { messages <- "ping" }() //continued //continued // Receive a value `<-channel` msg := <-messages fmt.Println(msg) } //Output ping
  29. 29. GC Easier for Programmer No overhead of memory allocation and freeing No explicit memory freeing
  30. 30. Interfaces Set of methods Methods on any type Data types implementing all methods satisfy
  31. 31. Thank You! SMITA VIJAYAKUMAR