37
26-27/05/2016 Go for Java Developers Stoyan Rachev May 26-27 ‘16, Sofia 1

Go for Java Developers jPrime 2016 - JUG for Java... · Go for Java Developers Stoyan Rachev May 26-27 ‘16, Sofia 1. 26-27/05/2016 Agenda • Introduction • Variables and Control

  • Upload
    others

  • View
    9

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Go for Java Developers jPrime 2016 - JUG for Java... · Go for Java Developers Stoyan Rachev May 26-27 ‘16, Sofia 1. 26-27/05/2016 Agenda • Introduction • Variables and Control

26-27/05/2016

Go for Java Developers

Stoyan RachevMay26-27‘16,Sofia

1

Page 2: Go for Java Developers jPrime 2016 - JUG for Java... · Go for Java Developers Stoyan Rachev May 26-27 ‘16, Sofia 1. 26-27/05/2016 Agenda • Introduction • Variables and Control

26-27/05/2016

Agenda

• Introduction• VariablesandControlFlow• TypesandDataStructures• Functions• MethodsandInterfaces• Concurrency• Conclusion

2

Page 3: Go for Java Developers jPrime 2016 - JUG for Java... · Go for Java Developers Stoyan Rachev May 26-27 ‘16, Sofia 1. 26-27/05/2016 Agenda • Introduction • Variables and Control

26-27/05/2016

WhatisGo?

GoisanopensourceprogramminglanguageoriginallyfromGooglethatmakesiteasytobuildsimple, reliable,andefficientsoftware.

3

Page 4: Go for Java Developers jPrime 2016 - JUG for Java... · Go for Java Developers Stoyan Rachev May 26-27 ‘16, Sofia 1. 26-27/05/2016 Agenda • Introduction • Variables and Control

26-27/05/2016

WhyGo?

• AnswertothechallengesofscaleatGoogle• Increasinglypopular(RedMonk,Tiobe)

4

Page 5: Go for Java Developers jPrime 2016 - JUG for Java... · Go for Java Developers Stoyan Rachev May 26-27 ‘16, Sofia 1. 26-27/05/2016 Agenda • Introduction • Variables and Control

26-27/05/2016

WhyGoforJavaDevelopers?

• Learningnewlanguagesmakesusbetterprogrammers• BetterchoicethanJavainsomesituations• ContributeorintegratewithexistingGoprojects

5

Page 6: Go for Java Developers jPrime 2016 - JUG for Java... · Go for Java Developers Stoyan Rachev May 26-27 ‘16, Sofia 1. 26-27/05/2016 Agenda • Introduction • Variables and Control

26-27/05/2016

GoandJava- Similarities

• C-style• Imperative• Compiled• Staticallytyped• Object-oriented• Cross-platform• Garbagecollection• Memorysafety• Typeassertions• Reflection

6

Page 7: Go for Java Developers jPrime 2016 - JUG for Java... · Go for Java Developers Stoyan Rachev May 26-27 ‘16, Sofia 1. 26-27/05/2016 Agenda • Introduction • Variables and Control

26-27/05/2016

GoandJava- Differences

• NoVM• Syntaxdifferences• Built-instrings,arrays,andmaps• Typeinference• Closures• Concurrency• Noclasses• Noinheritance• Noexceptions• Noannotations• Nogenerics

7

Page 8: Go for Java Developers jPrime 2016 - JUG for Java... · Go for Java Developers Stoyan Rachev May 26-27 ‘16, Sofia 1. 26-27/05/2016 Agenda • Introduction • Variables and Control

26-27/05/2016

WhySoDifferent?

• Languagedesign intheserviceofsoftwareengineering• Familiar,yetmodern• Highperformance,compileandruntime• Balancebetweencomplexityandconvenience• Emphasisonautomatedtools

Lessisexponentiallymore

8

Page 9: Go for Java Developers jPrime 2016 - JUG for Java... · Go for Java Developers Stoyan Rachev May 26-27 ‘16, Sofia 1. 26-27/05/2016 Agenda • Introduction • Variables and Control

26-27/05/2016

Example:Hello,world

9

Page 10: Go for Java Developers jPrime 2016 - JUG for Java... · Go for Java Developers Stoyan Rachev May 26-27 ‘16, Sofia 1. 26-27/05/2016 Agenda • Introduction • Variables and Control

26-27/05/2016

VariablesandConstants

// Javaint x;String s = "Hello, world!";public static final double PI = 3.14;

10

// Govar x ints := "Hello, world!"const Pi = 3.14

Page 11: Go for Java Developers jPrime 2016 - JUG for Java... · Go for Java Developers Stoyan Rachev May 26-27 ‘16, Sofia 1. 26-27/05/2016 Agenda • Introduction • Variables and Control

26-27/05/2016

ControlFlow- for

11

for i = 0; i < n; i++ { ...

}

for i < n {for {

Page 12: Go for Java Developers jPrime 2016 - JUG for Java... · Go for Java Developers Stoyan Rachev May 26-27 ‘16, Sofia 1. 26-27/05/2016 Agenda • Introduction • Variables and Control

26-27/05/2016

ControlFlow- if

12

if x < limit {...

} else {...

}

if x := math.Pow(a, n); x < limit {

Page 13: Go for Java Developers jPrime 2016 - JUG for Java... · Go for Java Developers Stoyan Rachev May 26-27 ‘16, Sofia 1. 26-27/05/2016 Agenda • Introduction • Variables and Control

26-27/05/2016

ControlFlow- switch

13

switch lang { case "java":

...case "go":

...default:

...}

switch true {...

}

switch lang := ui.Ask(); lang {

switch {

case getLanguage():

Page 14: Go for Java Developers jPrime 2016 - JUG for Java... · Go for Java Developers Stoyan Rachev May 26-27 ‘16, Sofia 1. 26-27/05/2016 Agenda • Introduction • Variables and Control

26-27/05/2016

BasicTypesandPointers

14

var s string

var r rune

var b boolean

var i int

var p *string = &s

Gotype Javatypeint8 byteint16 shortint32 intint64 longfloat32 floatfloat64 double

Page 15: Go for Java Developers jPrime 2016 - JUG for Java... · Go for Java Developers Stoyan Rachev May 26-27 ‘16, Sofia 1. 26-27/05/2016 Agenda • Introduction • Variables and Control

26-27/05/2016

ArraysandSlices

15

var a [2]stringa[0], a[1] = "java", "go"

var s []string = a[:]

s := []string{"java", "go"}

s := make([]string, 0)s = append(s, "java")var s []string

Page 16: Go for Java Developers jPrime 2016 - JUG for Java... · Go for Java Developers Stoyan Rachev May 26-27 ‘16, Sofia 1. 26-27/05/2016 Agenda • Introduction • Variables and Control

26-27/05/2016

Maps

16

m := make(map[string]int)m["java"], m["go"] = 1, 2

m := map[string]int{"java": 1, "go": 2}

r = m["go"]

delete(m, "go")

r, ok := m["ruby"] // ok is false

Page 17: Go for Java Developers jPrime 2016 - JUG for Java... · Go for Java Developers Stoyan Rachev May 26-27 ‘16, Sofia 1. 26-27/05/2016 Agenda • Introduction • Variables and Control

26-27/05/2016

Structs

17

type Point struct {X intY int

}

p := Point{1, 2}p.Y = 3p := &Point{1 , 2}

Page 18: Go for Java Developers jPrime 2016 - JUG for Java... · Go for Java Developers Stoyan Rachev May 26-27 ‘16, Sofia 1. 26-27/05/2016 Agenda • Introduction • Variables and Control

26-27/05/2016

Example:Meterstofeetconverter

18

Page 19: Go for Java Developers jPrime 2016 - JUG for Java... · Go for Java Developers Stoyan Rachev May 26-27 ‘16, Sofia 1. 26-27/05/2016 Agenda • Introduction • Variables and Control

26-27/05/2016

Functions

// Javapublic static int sum(int a, int b) {

return a + b;}

19

// Gofunc Sum(a int, b int) int {

return a + b}

Page 20: Go for Java Developers jPrime 2016 - JUG for Java... · Go for Java Developers Stoyan Rachev May 26-27 ‘16, Sofia 1. 26-27/05/2016 Agenda • Introduction • Variables and Control

26-27/05/2016

ErrorHandling

public static int f() throws Exception { // Java

...

throw MyException("...");

...

return 1

}

20

func f() (int, error) { // Go

...

return 0, fmt.Errorf("...")

...

return 1, nil

}

Page 21: Go for Java Developers jPrime 2016 - JUG for Java... · Go for Java Developers Stoyan Rachev May 26-27 ‘16, Sofia 1. 26-27/05/2016 Agenda • Introduction • Variables and Control

26-27/05/2016

ErrorHandling– CheckingforErrors

21

n, err := f()if err != nil {

...}// Use n...

Page 22: Go for Java Developers jPrime 2016 - JUG for Java... · Go for Java Developers Stoyan Rachev May 26-27 ‘16, Sofia 1. 26-27/05/2016 Agenda • Introduction • Variables and Control

26-27/05/2016

Closures

22

func fibonacci() func() int {x, y := 0, 1return func() int {

x, y = x + y, xreturn x

}}

f := fibonacci()for i := 0; i < 10; i++ {fmt.Println(f())

}

Page 23: Go for Java Developers jPrime 2016 - JUG for Java... · Go for Java Developers Stoyan Rachev May 26-27 ‘16, Sofia 1. 26-27/05/2016 Agenda • Introduction • Variables and Control

26-27/05/2016

defer

23

resp, err := http.Get(url)if err != nil {

...}defer resp.Body.Close()

Page 24: Go for Java Developers jPrime 2016 - JUG for Java... · Go for Java Developers Stoyan Rachev May 26-27 ‘16, Sofia 1. 26-27/05/2016 Agenda • Introduction • Variables and Control

26-27/05/2016

Methods

24

type Stack struct {data []string

}

func (s *Stack) Push(x string) {s.data = append(s.data, x)

}

s := NewStack()s.Push("Hello, world!")

Page 25: Go for Java Developers jPrime 2016 - JUG for Java... · Go for Java Developers Stoyan Rachev May 26-27 ‘16, Sofia 1. 26-27/05/2016 Agenda • Introduction • Variables and Control

26-27/05/2016

Interfaces

25

func Fprintf(w io.Writer, format string, args ...interface{}) (int, error)

type Writer interface {Write(p []byte) (int, error)

}

Page 26: Go for Java Developers jPrime 2016 - JUG for Java... · Go for Java Developers Stoyan Rachev May 26-27 ‘16, Sofia 1. 26-27/05/2016 Agenda • Introduction • Variables and Control

26-27/05/2016

Interfaces- Satisfaction

26

type NopWriter struct {}func (w *NopWriter) Write(p []byte) (int, error)

return len(p), nil}

var w NopWriterfmt.Fprintf(&w, "Hello, world!")

Page 27: Go for Java Developers jPrime 2016 - JUG for Java... · Go for Java Developers Stoyan Rachev May 26-27 ‘16, Sofia 1. 26-27/05/2016 Agenda • Introduction • Variables and Control

26-27/05/2016

Example:WordCount

27

Page 28: Go for Java Developers jPrime 2016 - JUG for Java... · Go for Java Developers Stoyan Rachev May 26-27 ‘16, Sofia 1. 26-27/05/2016 Agenda • Introduction • Variables and Control

26-27/05/2016

GoConcurrency

Don'tcommunicatebysharingmemory,sharememorybycommunicating.

28

Page 29: Go for Java Developers jPrime 2016 - JUG for Java... · Go for Java Developers Stoyan Rachev May 26-27 ‘16, Sofia 1. 26-27/05/2016 Agenda • Introduction • Variables and Control

26-27/05/2016

Goroutines

29

func say(s string) {fmt.Println(s)

}

go say("hi")say("there")

Page 30: Go for Java Developers jPrime 2016 - JUG for Java... · Go for Java Developers Stoyan Rachev May 26-27 ‘16, Sofia 1. 26-27/05/2016 Agenda • Introduction • Variables and Control

26-27/05/2016

Channels

30

ch := make(chan int) // create channel

ch <- x // send x = <-ch // receive and assign<-ch // receive and discard

close(ch) // close channel

Page 31: Go for Java Developers jPrime 2016 - JUG for Java... · Go for Java Developers Stoyan Rachev May 26-27 ‘16, Sofia 1. 26-27/05/2016 Agenda • Introduction • Variables and Control

26-27/05/2016

select

31

select {case <-ch1: // receive

...case x := <-ch2: // receive and assign

...case ch3 <- y: // send

...default:

...}

Page 32: Go for Java Developers jPrime 2016 - JUG for Java... · Go for Java Developers Stoyan Rachev May 26-27 ‘16, Sofia 1. 26-27/05/2016 Agenda • Introduction • Variables and Control

26-27/05/2016

Pipelines

32

numbers := make(chan int)

go func(int n) { // generatorfor i := 0; i < n; i++ {

numbers <- i}close(numbers)

}(10)

for x := range numbers { // printerfmt.Println(x)

}

Page 33: Go for Java Developers jPrime 2016 - JUG for Java... · Go for Java Developers Stoyan Rachev May 26-27 ‘16, Sofia 1. 26-27/05/2016 Agenda • Introduction • Variables and Control

26-27/05/2016

Example:Pipelines

33

Page 34: Go for Java Developers jPrime 2016 - JUG for Java... · Go for Java Developers Stoyan Rachev May 26-27 ‘16, Sofia 1. 26-27/05/2016 Agenda • Introduction • Variables and Control

26-27/05/2016

Tools

• gorun• gobuild• goinstall• gotest• goget• gofmt• goimports• godep

34

Page 35: Go for Java Developers jPrime 2016 - JUG for Java... · Go for Java Developers Stoyan Rachev May 26-27 ‘16, Sofia 1. 26-27/05/2016 Agenda • Introduction • Variables and Control

26-27/05/2016

WheretoGofromHere?

• GoWebsite,https://golang.org• ATourofGo,https://tour.golang.org• GoDocu,https://golang.org/doc/• GoFAQ,https://golang.org/doc/faq• “TheGoProgrammingLanguage”,byA.DonovanandB.Kernighan

35

Page 36: Go for Java Developers jPrime 2016 - JUG for Java... · Go for Java Developers Stoyan Rachev May 26-27 ‘16, Sofia 1. 26-27/05/2016 Agenda • Introduction • Variables and Control

26-27/05/2016

FinalWords

• GoissimilartoJava,yetdifferent• Goissimple,elegant, andefficient• Goisworthgivingatry!

36

Page 37: Go for Java Developers jPrime 2016 - JUG for Java... · Go for Java Developers Stoyan Rachev May 26-27 ‘16, Sofia 1. 26-27/05/2016 Agenda • Introduction • Variables and Control

26-27/05/2016

THANK YOU :)

Youcanfindmeat:@[email protected]

Stoyan RachevMay26-27 ’16,Sofia

37