Google Go illustrated on the basis of Fibonacci numbers

Introduction
Basics about Go
Example: Fibonacci numbers
Conclusion
Bibliography
Additional
Google Go
illustrated on the basis of
Fibonacci numbers
Jan Pennekamp
RWTH University Aachen
Chair for Data Management and Data Exploration
Prof. Dr. T. Seidl
Supervision: Dipl.-Ing. Marwan Hassani
Friday, 29st June 2012
slide 1 of 30
Jan Pennekamp
Google Go illustrated on the basis of Fibonacci numbers
Introduction
Basics about Go
Example: Fibonacci numbers
1
Introduction
2
Basics about Go
3
Example: Fibonacci numbers
4
Conclusion
5
Bibliography
6
Additional
slide 2 of 30
Jan Pennekamp
Conclusion
Bibliography
Additional
Google Go illustrated on the basis of Fibonacci numbers
Introduction
Basics about Go
Example: Fibonacci numbers
Conclusion
Bibliography
Additional
Main developers
Ken Thompson
* 1943
Unix shell
B (programming language)
UTF-8
slide 3 of 30
Jan Pennekamp
Google Go illustrated on the basis of Fibonacci numbers
Introduction
Basics about Go
Example: Fibonacci numbers
Conclusion
Bibliography
Additional
Main developers
Ken Thompson
* 1943
Rob Pike
Unix shell
B (programming language)
UTF-8
slide 3 of 30
Jan Pennekamp
* 1956
Plan 9 (operating system)
UTF-8
Google Go illustrated on the basis of Fibonacci numbers
Introduction
Basics about Go
Example: Fibonacci numbers
Conclusion
Bibliography
Additional
Go’s goals
today’s C
secure
fast to compile
easy to code
slide 4 of 30
Jan Pennekamp
Google Go illustrated on the basis of Fibonacci numbers
Introduction
Basics about Go
Example: Fibonacci numbers
Conclusion
Bibliography
Additional
Go’s goals
today’s C
secure
fast to compile
easy to code
for every project and environment
slide 4 of 30
Jan Pennekamp
Google Go illustrated on the basis of Fibonacci numbers
Introduction
Basics about Go
Example: Fibonacci numbers
Conclusion
Bibliography
Additional
Go’s goals
today’s C
secure
fast to compile
easy to code
for every project and environment
=> advantages of imperative and dynamic programming
slide 4 of 30
Jan Pennekamp
Google Go illustrated on the basis of Fibonacci numbers
Introduction
Basics about Go
Example: Fibonacci numbers
Conclusion
Bibliography
Additional
History of Go
open source project
financed by Google
slide 5 of 30
Jan Pennekamp
Google Go illustrated on the basis of Fibonacci numbers
Introduction
Basics about Go
Example: Fibonacci numbers
Conclusion
Bibliography
Additional
History of Go
open source project
financed by Google
initial design public release TIOBE award ’09
Sep. 2007
Nov. 2009
Jan. 2010
used at Google Go 1 release
May 2010
Mar. 2012
slide 5 of 30
Jan Pennekamp
Google Go illustrated on the basis of Fibonacci numbers
Introduction
Basics about Go
Example: Fibonacci numbers
Conclusion
Bibliography
Additional
Influences on Go
slide 6 of 30
Jan Pennekamp
Google Go illustrated on the basis of Fibonacci numbers
Introduction
Basics about Go
Example: Fibonacci numbers
Conclusion
Bibliography
Additional
Recent release: Go1
Windows compiler
final syntax
stable
converter for Go to Go1 code
slide 7 of 30
Jan Pennekamp
Google Go illustrated on the basis of Fibonacci numbers
Introduction
Basics about Go
Example: Fibonacci numbers
Conclusion
Bibliography
Additional
Hello World
1
2
3
4
5
6
7
8
slide 8 of 30
// Hello World program in Google Go
package main
import " fmt "
func main () {
fmt . Println ( " Hello world " )
}
Jan Pennekamp
Google Go illustrated on the basis of Fibonacci numbers
Introduction
Basics about Go
Example: Fibonacci numbers
Conclusion
Bibliography
Additional
Built-in data types
Integer
Float
Other
Boolean
slide 9 of 30
int8, int16, int32, uint8, ...
float32, float64
complex64, complex128, string, char, uchar
bool
Jan Pennekamp
Google Go illustrated on the basis of Fibonacci numbers
Introduction
Basics about Go
Example: Fibonacci numbers
Conclusion
Bibliography
Additional
Built-in data types
Integer
Float
Other
Boolean
int8, int16, int32, uint8, ...
float32, float64
complex64, complex128, string, char, uchar
bool
structs
anonymous structs
collections
slide 9 of 30
Jan Pennekamp
Google Go illustrated on the basis of Fibonacci numbers
Introduction
Basics about Go
Example: Fibonacci numbers
Conclusion
Bibliography
Additional
Loops implemented in Go
only for loops
slide 10 of 30
Jan Pennekamp
Google Go illustrated on the basis of Fibonacci numbers
Introduction
Basics about Go
Example: Fibonacci numbers
Conclusion
Bibliography
Additional
Loops implemented in Go
only for loops
1
2
3
4
5
6
7
8
9
10
11
slide 10 of 30
// regular for loop
for i := 0; i < 10; i ++ { ... }
// while loop
for ; sum < 1000; { ... }
for sum < 1000 { ... }
// infinite loop
for ; ; { ... }
for true { ... }
for { ... }
Jan Pennekamp
Google Go illustrated on the basis of Fibonacci numbers
Introduction
Basics about Go
Example: Fibonacci numbers
Conclusion
Bibliography
Additional
Control structures
complex switch command
break, continue
GoTo (contradiction to Go’s goals)
slide 11 of 30
Jan Pennekamp
Google Go illustrated on the basis of Fibonacci numbers
Introduction
Basics about Go
Example: Fibonacci numbers
Conclusion
Bibliography
Additional
Miscellaneous
pointer used for cbv, cbr
no real pointer aritmetic
garbage collector
slide 12 of 30
Jan Pennekamp
Google Go illustrated on the basis of Fibonacci numbers
Introduction
Basics about Go
Example: Fibonacci numbers
Conclusion
Bibliography
Additional
Miscellaneous
pointer used for cbv, cbr
no real pointer aritmetic
garbage collector
package system, interfaces
godoc, gofmt
slide 12 of 30
Jan Pennekamp
Google Go illustrated on the basis of Fibonacci numbers
Introduction
Basics about Go
Example: Fibonacci numbers
Conclusion
Bibliography
Additional
Overview
important example in computer science
Pascal & Java commonly known
slide 13 of 30
Jan Pennekamp
Google Go illustrated on the basis of Fibonacci numbers
Introduction
Basics about Go
Example: Fibonacci numbers
Conclusion
Bibliography
Additional
Overview
important example in computer science
Pascal & Java commonly known
both with influence on Go
slide 13 of 30
Jan Pennekamp
Google Go illustrated on the basis of Fibonacci numbers
Introduction
Basics about Go
Example: Fibonacci numbers
Conclusion
Bibliography
Additional
Overview
important example in computer science
Pascal & Java commonly known
both with influence on Go
shows advantages and easyness of Go
slide 13 of 30
Jan Pennekamp
Google Go illustrated on the basis of Fibonacci numbers
Introduction
Basics about Go
Example: Fibonacci numbers
Conclusion
Bibliography
Additional
If-else chains
1 // if - else chain - exception handling
2 switch {
3
case n <= 0:
4
return 0 , 1 , err . New ( " Number lower / equal
zero , try again . " )
5
case n >= 100:
6
return 0 , 1 , err . New ( " Number higher / equal
100 , try again . " )
7 }
slide 14 of 30
Jan Pennekamp
Google Go illustrated on the basis of Fibonacci numbers
Introduction
Basics about Go
Example: Fibonacci numbers
Conclusion
Bibliography
Additional
Multiple return values
1 // function declaration
2 func fib (a , b int64 , n int ) ( int64 , int64 ,
error ) {
3 // name parameters
return values
4
5 // function call
6
_ , res , err :=
fib (0 ,1 , n )
7 // return values name parameters
slide 15 of 30
Jan Pennekamp
Google Go illustrated on the basis of Fibonacci numbers
Introduction
Basics about Go
Example: Fibonacci numbers
Conclusion
Bibliography
Additional
Multiple assignments
1 // multiple assignments in Go
2 b , a = a +b , b
3 // uses the values of the variables before the
call
slide 16 of 30
Jan Pennekamp
Google Go illustrated on the basis of Fibonacci numbers
Introduction
Basics about Go
Example: Fibonacci numbers
Conclusion
Bibliography
Additional
Rating
advantages:
keeps it goals
has a future
in development
modern language
slide 17 of 30
Jan Pennekamp
Google Go illustrated on the basis of Fibonacci numbers
Introduction
Basics about Go
Example: Fibonacci numbers
Conclusion
Bibliography
Additional
Rating
advantages:
keeps it goals
has a future
in development
modern language
slide 17 of 30
disadvantages:
assoziated with Google
hardly literature
Jan Pennekamp
Google Go illustrated on the basis of Fibonacci numbers
Introduction
Basics about Go
Example: Fibonacci numbers
Conclusion
Bibliography
Additional
Own opinion
easy to learn
for every project
fast compile time
has a future
slide 18 of 30
Jan Pennekamp
Google Go illustrated on the basis of Fibonacci numbers
Introduction
Basics about Go
Example: Fibonacci numbers
Conclusion
Bibliography
Additional
Own opinion
easy to learn
for every project
fast compile time
has a future
=> good requirements to become today’s C
slide 18 of 30
Jan Pennekamp
Google Go illustrated on the basis of Fibonacci numbers
Introduction
Basics about Go
Example: Fibonacci numbers
Conclusion
Bibliography
Additional
Additional features
goroutines
channels
native network support
...
slide 19 of 30
Jan Pennekamp
Google Go illustrated on the basis of Fibonacci numbers
Introduction
Basics about Go
Example: Fibonacci numbers
Conclusion
Bibliography
Additional
Questions
Thank you for your attention!
Any questions?
slide 20 of 30
Jan Pennekamp
Google Go illustrated on the basis of Fibonacci numbers
Introduction
Basics about Go
Example: Fibonacci numbers
Conclusion
Bibliography
Additional
Bibliography - Books
books:
Ivo Balbaert. The Way to Go - A
Thorough Introduction to the Go
Programming Language
Rainer Feike and Steffen Blass.
Programmierung in Google Go
Frank Müller. Systemprogrammierung
in Google Go - GrundlagenSkalierbarkeit- Performanz- Sicherheit
slide 21 of 30
Jan Pennekamp
Google Go illustrated on the basis of Fibonacci numbers
Introduction
Basics about Go
Example: Fibonacci numbers
Conclusion
Bibliography
Additional
Bibliography - Books
internet:
http://www.gommunity.de/
http://golang.org
http://youtube.com/watch?v=wwoWei-GAPo
http://en.wikipedia.org/wiki/Ken_Thompson
http://en.wikipedia.org/wiki/Rob_Pike
slide 22 of 30
Jan Pennekamp
Google Go illustrated on the basis of Fibonacci numbers
Introduction
Basics about Go
Example: Fibonacci numbers
Conclusion
Bibliography
Additional
Structs
1
2
3
4
5
6
7
8
9
slide 23 of 30
type example struct {
number int
name string
}
...
var (
valueA example = example {1 , " one " }
valueB example = example {2 , " two " }
)
Jan Pennekamp
Google Go illustrated on the basis of Fibonacci numbers
Introduction
Basics about Go
Example: Fibonacci numbers
Conclusion
Bibliography
Additional
Package import
1
2
3
4
5
6
7
8
slide 24 of 30
import (
. " fmt "
err " errors "
)
...
err . New ( " Number lower / equal zero , try again . " )
...
Printf ( " The fibonacci number of % v is % v \ n " ,n ,
res )
Jan Pennekamp
Google Go illustrated on the basis of Fibonacci numbers
Introduction
Basics about Go
Example: Fibonacci numbers
Conclusion
Bibliography
Additional
Google Go version
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
slide 25 of 30
p a c k a g e main
// p a c k a g e i m p o r t
import (
. ” fmt ”
err ” errors ”
)
// f u n c t i o n w i t h 3 r e t u r n v a l u e s
func f i b (a , b int64 , n i n t ) ( int64 , int64 , e r r o r ) {
// i f −e l s e c h a i n − e x c e p t i o n h a n d l i n g
switch {
c a s e n <= 0 :
r e t u r n 0 , 1 , e r r . New ( ”Number l o w e r / e q u a l z e r o , t r y a g a i n . ” )
c a s e n >= 1 0 0 :
r e t u r n 0 , 1 , e r r . New ( ”Number h i g h e r / e q u a l 1 0 0 , t r y a g a i n . ” )
}
// c a l c u l a t i o n
f o r i := i n t ( b ) ; i < n ; i++ {
b , a = a+b , b
}
return a , b , n i l
}
Jan Pennekamp
Google Go illustrated on the basis of Fibonacci numbers
Introduction
Basics about Go
Example: Fibonacci numbers
Conclusion
Bibliography
Additional
Google Go version (continuation)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
slide 26 of 30
// main p r o c e d u r e
f u n c main ( ) {
var n i n t
f . P r i n t f ( ” T h i s program c a l c u l a t e s t h e f i b o n a c c i numbers i t e r a t i v e . \ n” )
// w h i l e l o o p
for {
f . P r i n t f ( ” Which f i b o n a c c i number s h o u l d be computed ?\n” )
f . S c a n f ( ”%d” , &n )
// f u n c t i o n c a l l
, r e s , e r r := f i b ( 0 , 1 , n )
i f e r r != n i l {
f . Println ( err )
// e r r o r o u t p u t
} else {
f . P r i n t f ( ”The f i b o n a c c i number o f %v i s %v\n” , n , r e s ) // r e s u l t o u t p u t
}
}
}
// 707 c h a r a c t e r s w i t h o u t comments
Jan Pennekamp
Google Go illustrated on the basis of Fibonacci numbers
Introduction
Basics about Go
Example: Fibonacci numbers
Conclusion
Bibliography
Additional
Java version
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
slide 27 of 30
public class Fibonacci {
// f u n c t i o n
public s t a t i c long f i b ( i n t n)
throws FibonacciNegativeException , FibonacciTooBigException {
// i f −e l s e c h a i n − e x c e p t i o n h a n d l i n g
i f ( n <= 0 )
t h r o w new F i b o n a c c i N e g a t i v e E x c e p t i o n ( ) ;
i f ( n >= 1 0 0 )
t h r o w new F i b o n a c c i T o o B i g E x c e p t i o n ( ) ;
// c a l c u l a t i o n
long a = 0 , b = 1 , c ;
f o r ( i n t i = 2 ; i <= n ; i ++) {
c = a + b;
a = b;
b = c;
}
return b ;
}
Jan Pennekamp
Google Go illustrated on the basis of Fibonacci numbers
Introduction
Basics about Go
Example: Fibonacci numbers
Conclusion
Bibliography
Additional
Java version (continuation)
1
2
3
4
5
6
7
8
9
10
// main p r o c e d u r e
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
System . o u t . p r i n t l n ( ” T h i s program c a l c u l a t e s t h e f i b o n a c c i numbers
i t e r a t i v e . ”) ;
// w h i l e l o o p
while ( true ) {
System . o u t . p r i n t l n ( ” Which f i b o n a c c i number s h o u l d be computed ? ” ) ;
i n t n =I n t e g e r . p a r s e I n t ( System . c o n s o l e ( ) . r e a d L i n e ( ) ) ;
System . o u t . p r i n t l n ( ) ;
// r e s u l t o u t p u t
t r y { System . o u t . p r i n t l n ( ”The f i b o n a c c i number o f ” + n + ” i s ” +
fib (n) ) ; }
c a t c h ( F i b o n a c c i N e g a t i v e E x c e p t i o n f n e ) // e r r o r o u t p u t
{System . o u t . p r i n t l n ( ”Number l o w e r / e q u a l z e r o , t r y a g a i n . ” ) ; }
c a t c h ( F i b o n a c c i T o o B i g E x c e p t i o n f t b e ) // e r r o r o u t p u t
{System . o u t . p r i n t l n ( ”Number h i g h e r / e q u a l 1 0 0 , t r y a g a i n . ” ) ; }
System . o u t . p r i n t l n ( ) ;
}
}
11
12
13
14
15
16
17
18 }
19 // 1020 c h a r a c t e r s w i t h o u t comments
slide 28 of 30
Jan Pennekamp
Google Go illustrated on the basis of Fibonacci numbers
Introduction
Basics about Go
Example: Fibonacci numbers
Conclusion
Bibliography
Additional
Pascal version
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
slide 29 of 30
PROGRAM f i b o n a c c i ;
USES c r t ; // p a c k a g e i m p o r t
VAR n : INTEGER ;
res
: LONGINT ;
// f u n c t i o n
FUNCTION F i b o n a c c i ( n : INTEGER ) : LONGINT ;
VAR i : INTEGER ;
a , b , c : LONGINT ;
BEGIN
// c a l c u l a t i o n
a := 0 ; b := 1 ;
FOR i := 2 TO n DO
BEGIN
c := a + b ;
a := b ;
b := c ;
END ;
F i b o n a c c i := b ;
END ;
Jan Pennekamp
Google Go illustrated on the basis of Fibonacci numbers
Introduction
Basics about Go
Example: Fibonacci numbers
Conclusion
Bibliography
Additional
Pascal version (continuation)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
slide 30 of 30
// main p r o c e d u r e
BEGIN
W r i t e l n ( ’ T h i s program c a l c u l a t e s t h e f i b o n a c c i numbers i t e r a t i v e . ’ ) ;
// w h i l e l o o p
WHILE ( t r u e ) DO
BEGIN
REPEAT
W r i t e l n ( ’ Which f i b o n a c c i number s h o u l d be computed ? ’ ) ;
Readln ( n ) ;
// e x c e p t i o n h a n d l i n g
I F ( n <= 0 )
THEN W r i t e l n ( ’ Number l o w e r / e q u a l z e r o , t r y a g a i n . ’ )
ELSE I F ( n >= 1 0 0 )
THEN W r i t e l n ( ’ Number h i g h e r / e q u a l 1 0 0 , t r y a g a i n . ’ ) ;
UNTIL ( n > 0 ) AND ( n < 1 0 0 ) ;
Writeln ;
// f u n c t i o n c a l l
r e s := F i b o n a c c i ( n ) ;
W r i t e l n ( ’ The f i b o n a c c i number o f ’ , n , ’ i s ’ , r e s ) ; // r e s u l t o u t p u t
Writeln ;
END ;
END .
// 760 c h a r a c t e r s w i t h o u t comments
Jan Pennekamp
Google Go illustrated on the basis of Fibonacci numbers