Upload
others
View
9
Download
0
Embed Size (px)
Citation preview
26-27/05/2016
Go for Java Developers
Stoyan RachevMay26-27‘16,Sofia
1
26-27/05/2016
Agenda
• Introduction• VariablesandControlFlow• TypesandDataStructures• Functions• MethodsandInterfaces• Concurrency• Conclusion
2
26-27/05/2016
WhatisGo?
GoisanopensourceprogramminglanguageoriginallyfromGooglethatmakesiteasytobuildsimple, reliable,andefficientsoftware.
3
26-27/05/2016
WhyGo?
• AnswertothechallengesofscaleatGoogle• Increasinglypopular(RedMonk,Tiobe)
4
26-27/05/2016
WhyGoforJavaDevelopers?
• Learningnewlanguagesmakesusbetterprogrammers• BetterchoicethanJavainsomesituations• ContributeorintegratewithexistingGoprojects
5
26-27/05/2016
GoandJava- Similarities
• C-style• Imperative• Compiled• Staticallytyped• Object-oriented• Cross-platform• Garbagecollection• Memorysafety• Typeassertions• Reflection
6
26-27/05/2016
GoandJava- Differences
• NoVM• Syntaxdifferences• Built-instrings,arrays,andmaps• Typeinference• Closures• Concurrency• Noclasses• Noinheritance• Noexceptions• Noannotations• Nogenerics
7
26-27/05/2016
WhySoDifferent?
• Languagedesign intheserviceofsoftwareengineering• Familiar,yetmodern• Highperformance,compileandruntime• Balancebetweencomplexityandconvenience• Emphasisonautomatedtools
Lessisexponentiallymore
8
26-27/05/2016
Example:Hello,world
9
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
26-27/05/2016
ControlFlow- for
11
for i = 0; i < n; i++ { ...
}
for i < n {for {
26-27/05/2016
ControlFlow- if
12
if x < limit {...
} else {...
}
if x := math.Pow(a, n); x < limit {
26-27/05/2016
ControlFlow- switch
13
switch lang { case "java":
...case "go":
...default:
...}
switch true {...
}
switch lang := ui.Ask(); lang {
switch {
case getLanguage():
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
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
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
26-27/05/2016
Structs
17
type Point struct {X intY int
}
p := Point{1, 2}p.Y = 3p := &Point{1 , 2}
26-27/05/2016
Example:Meterstofeetconverter
18
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}
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
}
26-27/05/2016
ErrorHandling– CheckingforErrors
21
n, err := f()if err != nil {
...}// Use n...
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())
}
26-27/05/2016
defer
23
resp, err := http.Get(url)if err != nil {
...}defer resp.Body.Close()
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!")
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)
}
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!")
26-27/05/2016
Example:WordCount
27
26-27/05/2016
GoConcurrency
Don'tcommunicatebysharingmemory,sharememorybycommunicating.
28
26-27/05/2016
Goroutines
29
func say(s string) {fmt.Println(s)
}
go say("hi")say("there")
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
26-27/05/2016
select
31
select {case <-ch1: // receive
...case x := <-ch2: // receive and assign
...case ch3 <- y: // send
...default:
...}
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)
}
26-27/05/2016
Example:Pipelines
33
26-27/05/2016
Tools
• gorun• gobuild• goinstall• gotest• goget• gofmt• goimports• godep
34
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
26-27/05/2016
FinalWords
• GoissimilartoJava,yetdifferent• Goissimple,elegant, andefficient• Goisworthgivingatry!
36