Go for Java Developers Stoyan Rachev May 26-27 16, Sofia 1
Agenda Introduction Variables and Control Flow Types and Data Structures Functions Methods and Interfaces Concurrency Conclusion 2
What is Go? Go is an open source programming language originally from Google that makes it easy to build simple, reliable, and efficient software. 3
Why Go? Answer to the challenges of scale at Google Increasingly popular (RedMonk, Tiobe) 4
Why Go for Java Developers? Learning new languages makes us better programmers Better choice than Java in some situations Contribute or integrate with existing Go projects 5
Go and Java - Similarities C-style Imperative Compiled Statically typed Object-oriented Cross-platform Garbage collection Memory safety Type assertions Reflection 6
Go and Java - Differences No VM Syntax differences Built-in strings, arrays, and maps Type inference Closures Concurrency No classes No inheritance No exceptions No annotations No generics 7
Why So Different? Language design in the service of software engineering Familiar, yet modern High performance, compile and runtime Balance between complexity and convenience Emphasis on automated tools Less is exponentially more 8
Example: Hello, world 9
Variables and Constants // Java int x; String s = "Hello, world!"; public static final double PI = 3.14; // Go var x int s := "Hello, world!" const Pi = 3.14 10
Control Flow - for for i { < = n 0; { i < n; i++ {... 11
Control Flow - if if x < := limit math.pow(a, { n); x < limit {... else {... 12
Control Flow - switch switch lang {:= ui.ask(); lang { case "java": getlanguage():... case "go":... default:... switch true { {... 13
Basic Types and Pointers var s string var r rune var b boolean Go type int8 int16 int32 int64 float32 float64 Java type byte short int long float double var i int var p *string = &s 14
Arrays and Slices var a [2]string a[0], a[1] = "java", "go" var s []string = a[:] s := []string{"java", "go" s var:= s make([]string, 0) s = append(s, "java") 15
Maps m := make(map[string]int) m["java"], m["go"] = 1, 2 m := map[string]int{"java": 1, "go": 2 r, = ok m["go"] := m["ruby"] // ok is false delete(m, "go") 16
Structs type Point struct { X int Y int p := Point{1, &Point{1 2, 2 p.y = 3 17
Example: Meters to feet converter 18
Functions // Java public static int sum(int a, int b) { return a + b; // Go func Sum(a int, b int) int { return a + b 19
Error Handling public static int f() throws Exception { // Java... throw MyException("...");... return 1 func f() (int, error) { // Go... return 0, fmt.errorf("...")... return 1, nil 20
Error Handling Checking for Errors n, err := f() if err!= nil {... // Use n... 21
Closures func fibonacci() func() int { x, y := 0, 1 return func() int { x, y = x + y, x return x f := fibonacci() for i := 0; i < 10; i++ { fmt.println(f()) 22
defer resp, err := http.get(url) if err!= nil {... defer resp.body.close() 23
Methods type Stack struct { data []string func (s *Stack) Push(x string) { s.data = append(s.data, x) s := NewStack() s.push("hello, world!") 24
Interfaces func Fprintf(w io.writer, format string, args...interface{) (int, error) type Writer interface { Write(p []byte) (int, error) 25
Interfaces - Satisfaction type NopWriter struct { func (w *NopWriter) Write(p []byte) (int, error) return len(p), nil var w NopWriter fmt.fprintf(&w, "Hello, world!") 26
Example: Word Count 27
Go Concurrency Don't communicate by sharing memory, share memory by communicating. 28
Goroutines func say(s string) { fmt.println(s) go say("hi") say("there") 29
Channels ch := make(chan int) // create channel ch <- x // send x = <-ch // receive and assign <-ch // receive and discard close(ch) // close channel 30
select select { case <-ch1: // receive... case x := <-ch2: // receive and assign... case ch3 <- y: // send... default:... 31
Pipelines numbers := make(chan int) go func(int n) { // generator for i := 0; i < n; i++ { numbers <- i close(numbers) (10) for x := range numbers { // printer fmt.println(x) 32
Example: Pipelines 33
Tools go run go build go install go test go get gofmt goimports godep 34
Where to Go from Here? Go Web site, https://golang.org A Tour of Go, https://tour.golang.org Go Docu, https://golang.org/doc/ Go FAQ, https://golang.org/doc/faq The Go Programming Language, by A. Donovan and B. Kernighan 35
Final Words Go is similar to Java, yet different Go is simple, elegant, and efficient Go is worth giving a try! 36
THANK YOU :) You can find me at: @stoyanr stoyanr@gmail.com Stoyan Rachev May 26-27 16, Sofia 37