0% found this document useful (0 votes)
85 views41 pages

Go Lecture PDF

Go is a language invented by Google. Its full of Object oriented. We can say it as it is an easy and secure way of programming language.

Uploaded by

Dharshan Raj
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views41 pages

Go Lecture PDF

Go is a language invented by Google. Its full of Object oriented. We can say it as it is an easy and secure way of programming language.

Uploaded by

Dharshan Raj
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

Some Trucs and Machins about Google Go

Narbel
The Cremis Saturdays, Saison I
University of Bordeaux 1

April 2010
(v.1.01)

1 / 41

A New Language in a Computerizationed World

A magnicent quartet...:
Microsoft: Windows, Internet Explorer (Gazelle?), Microsoft Oce, Windows Mobile, C#/F#. Sun: Solaris, HotJava, StarOce, SavaJe, Java. Apple: MacOS, Safari, iWork, iPhoneOS, Objective-C. Google: ChromeOS, Chrome, Google Docs, Android, Go.

Go: the last brick (born in November 2009).

2 / 41

Objective-C and Go: a new kind of progression...(from www.tiobe.com, April 2010)


3 / 41

Creating New Languages and Tactics...

Securing languages for oneself, a modern tactics? e.g.:


Java C# (Microsoft). OCaml F# (Microsoft). C Go ? (Google).

In the Go team, some C/Unix-stars:


Ken Thompson (Multics, Unix, B, Plan 9, ed, UTF-8, etc. Turing Award). Rob Pike (Plan 9, Inferno, Limbo, UTF-8, etc.)

4 / 41

One of the Underlying Purposes of Go...!?


Many recent successful languages are dynamic-oriented, i.e. Python, Ruby, etc. or extensions/avatars of Java, like Clojure and Groovy. In the ocial Go tutorial: It feels like a dynamic language but has the speed and safety of a static language. Even if there exist dynamic languages with very ecient compilers (cf. CLOS), Go takes a step out of the current dynamic-oriented trend, and proposes more type-safe programming, while at once focussing on fast compilation and code execution... (i.e. towards high-performance web programming?).
5 / 41

Apart e: Compiled, Interpreted, Tomato-Souped...


In A Tutorial for the Go Programming Language, one nds Go is a compiled language... (no more meaning than interpreted language...). For instance, consider the Wikipedia entry about Python:
At rst: Python is an interpreted, interactive programming language created by Guido van Rossum. Next (at 16:37, 16 Sept. 2006), the truth took over...: Python is a programming language created by Guido van Rossum in 1990. Python is fully dynamically typed and uses automatic memory management. [...] The de facto standard for the language is the CPython implementation, which is a bytecode compiler and interpreter [...] run the program using the Psyco just-in-time compiler.
6 / 41

References about Go
Bibliography about Go is still rather short...: Ocial doc (on golang.org):
The Go Programming Language Specication. Package Documentation (the standard lib doc). A Tutorial for the Go Programming Language. Eective Go : the main text on the ocial site (could be
better...)

Various FAQs and


groups.google.com/group/golang-nuts/.

The Go Programming Language, slides of Rob Pike, 2009.

A lot of bloggy internet stu...


7 / 41

Yet Another Hello Hello

package main i m p o r t fmt // f o r m a t t e d I /O. f u n c main ( ) { fmt . Printf ( H e l l o H e l l o \ n ) }

8 / 41

Echo Function in Go
package main import ( os f l a g // command l i n e o p t i o n p a r s e r ) v a r omitNewline = flag . Bool ( n , false , no f i n a l const ( Space = Newline = \ n ) newline )

f u n c main ( ) { flag . Parse ( ) // S c a n s t h e a r g l i s t and s e t s up f l a g s var s s t r i n g = f o r i := 0 ; i < flag . NArg ( ) ; i++ { if i > 0 { s += Space } s += flag . Arg ( i ) } i f ! omitNewline { s += Newline } os . Stdout . WriteString ( s ) }

9 / 41

Echo Function in C

(not much of a dierence...)

int main ( i n t argc , c h a r argv [ ] ) { i n t nflag ; i f (++ argv && ! strcmp ( argv , n ) ) { ++argv ; nflag = 1 ; } else nflag = 0 ; w h i l e ( argv ) { ( v o i d ) printf ( %s , argv ) ; i f (++ argv ) ( v o i d ) putchar ( ) ; } i f ( nflag == 0 ) ( v o i d ) putchar ( \ n ) ; fflush ( stdout ) ; i f ( ferror ( stdout ) ) exit ( 1 ) ; exit ( 0 ) ; }
10 / 41

Some Interesting Points about Go...

A language should not in general be compared from its syntax, sugar gadgets, construct peculiarities... (we skip them). And even if Go does not include revolutionary ideas, there are some points worth to be discussed, e.g.:
Polymorphic functions (CLOS-Haskell-like). Typed functional programming. Easy to use multi-threading.

11 / 41

Functions
Basic function header:
f u n c < function name >(< typed args >) < r e t u r n type > {}

Basic function header with multiple returns:


f u n c < function name >(< typed args >) (< r e t u r n types >) {}

For instance:
func f ( i n t i ) ( int , s t r i n g ) { r e t u r n ( i +1) ( s u p e r g e n i a l ) } x , y := f ( 3 ) ;

12 / 41

Functions with Multiple Return Values


A common use of functions with multiple return values: error management... (there are no exceptions in Go). For instance:
f u n c ReadFull ( r Reader , buf [ ] b y t e ) ( n i n t , err os . Error ) { f o r len ( buf ) > 0 && err == nil { v a r nr i n t ; nr , err = r . Read ( buf ) ; n += nr ; buf = buf [ nr : len ( buf ) ] ; } return ; }

13 / 41

Functions Associated to a Type


Header for a function to be associated to a type T:
f u n c (< arg > T ) <functionName >(< typed args >) < r e t u r n type > {}

The argument arg of type T can be used in the body of the function like the other arguments. For instance:
t y p e Point s t r u c t { x , y float64 } f u n c ( p Point ) Norm ( ) float64 { r e t u r n math . Sqrt ( p . x p . x + p . y p . y ) ; } p := Point { 1 , 2 } ; [ . . . ] p . Norm ( ) [ . . . ] ;

Similar syntax to C++/Java: the associated type argument is privileged, as is an object rel. to its methods.
14 / 41

Interfaces : Towards Polymorphism


An example of interface:
t y p e Truc i n t e r f a c e { F1 ( ) i n t ; F2 ( i n t , i n t ) ; }

Truc is then a registered type (in fact, a type of types). Rule: Every data type T with associated functions as declared in the interface Truc is compatible with Truc. Every instance of type T is also an instance of type Truc. A type can satisfy more than one interface.
15 / 41

Interfaces : Towards Polymorphism


t y p e PrintableTruc i n t e r f a c e { PrintStandard ( ) ; } t y p e T1 s t r u c t { a i n t ; b f l o a t ; c s t r i n g ; } t y p e T2 s t r u c t { x i n t ; y i n t ; } f u n c ( t1 T1 ) PrintStandard ( ) { fmt . Printf ( %d %g %q \ n , t1 . a , t1 . b , t1 . c ) ; } f u n c ( t2 T2 ) PrintStandard ( ) { fmt . Printf ( %d %d \ n , t2 . x , t2 . y ) ; } f u n c F ( p PrintableTruc ) { p . PrintStandard ( ) ; } // p o l y m o r p h i c

f u n c main ( ) { nuple1 := &T1 { 7 , 2.35 , b l u e b e r r i e s } ; nuple2 := &T2 { 1 , 2 } ; nuple1 . PrintStandard ( ) ; F ( nuple1 ) ; // o u t p u t : 7 2.35 b l u e b e r r i e s F ( nuple2 ) ; // o u t p u t : 1 2 }

16 / 41

Type compatibility and Upcasts

Type compatibility and type-safe implicit upcasting:


[...] t y p e T3 s t r u c t { d f l o a t } [...] v a r nuple PrintableTruc ; nuple1 := &T1 { 7 , 2.35 , b l u e b e r r i e s } ; nuple3 := &T3 { 1 . 4 3 2 4 2 } ; nuple = nuple1 // ok nuple = nuple3 ; // s t a t i c a l l y n o t ok : T3 i s n o t P r i n t a b l e T r u c

17 / 41

Dynamic Selection

Function selection can be dynamic (and still type-safe):


f u n c main ( ) { v a r nuple PrintableTruc ; nuple1 := &T1 { 7 , 2.35 , b l u e b e r r i e s } ; nuple2 := &T2 { 1 , 2 } ; f o r i := 0 ; i < 1 0 ; i++ { i f ( rand . Int ( ) %2) == 1 { nuple = nuple1 } e l s e { nuple = nuple2 } nuple . PrintStandard ( ) ; } }

18 / 41

How to Use Existing Interfaces: a Basic Example


In the package sort, there is an interface denition:
t y p e Interface i n t e r f a c e { Len ( ) i n t Less ( i , j i n t ) bool Swap ( i , j i n t ) }

(idiomatic: here, the complete name is sort.Interface).

There is also a polymorphic function:


f u n c Sort ( data Interface ) { f o r i := 1 ; i < data . Len ( ) ; i++ { f o r j := i ; j > 0 && data . Less ( j , j 1); j { data . Swap ( j , j 1) } } }

19 / 41

How to Use Existing Interfaces: a Basic Example


Using sorting for a user-dened type:
i m p o r t ( fmt sort ) t y p e IntSequence [ ] i n t // I n t S e q u e n c e a s a s o r t . I n t e r f a c e : f u n c ( p IntSequence ) Len ( ) i n t { r e t u r n len ( p ) } f u n c ( p IntSequence ) Less ( i , j i n t ) bool { r e t u r n p [ i ] < p [ j ] } f u n c ( p IntSequence ) Swap ( i , j i n t ) { p [ i ] , p [ j ] = p [ j ] , p [ i ] } f u n c main ( ) { v a r data IntSequence = [ ] i n t { 1 , 3 , 4 , 1, 5 , 333 } sort . Sort ( data ) // d a t a c a l l e d a s a s o r t . I n t e r f a c e fmt . Println ( data ) ; }

Output:
[ 1 1 3 4 5 3 3 3 ]

20 / 41

Maximum Polymorphism and Reection


In C: maximum polymorphism through void*. In Go: maximum polymorphism through the empty interface, i.e. interface {}. For example, the printing functions in fmt use it. Need for some reection mechanisms, i.e. ways to check at runtime that instances satisfy types, or are associated to functions. For instance, to check that x0 satises the interface I:
x1 , ok := x0 . ( I ) ;

(ok is a boolean, and if true, x1 is x0 with type I)


21 / 41

Duck Typing
Go functional polymorphism is a type-safe realization of duck typing. Implicit Rule: If something can do this, then it can be used here. Naively: When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck. Opportunistic behavior of the type instances. Dynamic OO languages like CLOS or Groovy include duck typing in a natural way. In static languages: duck typing is realized as a structural typing mechanism (instead of nominal in which all type compatibilities should be made explicit see e.g., implements, extends in Java).
22 / 41

Go Interfaces and Structuration Levels


Go interfaces A type-safe overloading mechanism where sets of overloaded functions make type instances compatible or not to the available types (interfaces). The eect of an expression like:
x.F(..)

depends on all the available denitions of F, on the type of x, and on the set of available interfaces where F occurs. About the grain of structuration dilemma between the functional and modular levels: Go votes for the functional level, but less than CLOS, a little more than Haskell, and denitely more than Java/C# (where almost every type is implemented as an encapsulating class)...
23 / 41

Other Languages
Go interface-based mechanism is not new, neither very powerful... For instance, Haskell type classes:
c l a s s SmallClass a where f1 : : a > a f2 : : a > Int // s i m i l a r t o a Go i n t e r f a c e

i n s t a n c e SmallClass Char where f1 x = chr ( ( ord x ) + 1 ) f2 = ord i n s t a n c e SmallClass Bool where f1 _ = True f2 True = 1 f2 False = 0

// i m p l e m e n t a t i o n

// a n o t h e r i m p l e m e n t a t i o n

Haskell oers type inference with constrained genericity, and inheritance...


24 / 41

Other Languages
Go structural-oriented type system is not new, neither very powerful... For instance, inferred Ocaml open object types:
# let f x = ( x#f1 1 ) | | ( x#f2 a ) ; ; // v a l f : < f 1 : i n t > b o o l ; // f 2 : c h a r > b o o l ;

. . > > b o o l

OCaml oers type and interface inference with constrained genericity, and inheritance...

25 / 41

Interface Unions
In Go, no explicit inheritance mechanism. The only possibility: some implicit behavior inheritance through interface unions (called embedding):
t y p e Truc1 i n t e r f a c e { F1 ( ) i n t ; } t y p e Truc2 i n t e r f a c e { F2 ( ) i n t ; } t y p e Truc i n t e r f a c e { Truc1 ; // i n c l u s i o n f Truc2 ; // i n c l u s i o n }

Rule: If type T is compatible with Truc, it is compatible with Truc1 and Truc2 too.
26 / 41

Functional Programming
Functional programming (FP) has two related characteristics:
First-class functions. Functions/methods are rst-class citizens, i.e. they can be (0) named by a variable; (1) passed to a function as an argument; (2) returned from a function as a result; (3) stored in any kind of data structure. Closures. Functions/methods denitions are associated to some/all of the environment when they are dened.

When these are available Most of the FP techniques are possible Caveat: Only full closures can ensure pure FP (and in particular, referential transparency).
27 / 41

Go Functions are First-Class


t y p e IntFun f u n c ( i n t ) i n t ; f u n c funpassing ( f0 IntFun ) { fmt . Printf ( P a s s e d : %d \ n , f0 ( 1 0 0 ) ) ; } f u n c funproducer ( i i n t ) IntFun { r e t u r n f u n c ( j i n t ) i n t { r e t u r n i+j ; } } f u n c main ( ) { f := f u n c ( i i n t ) i n t { r e t u r n i + 1 0 0 0 ; } // anonymous f u n c t i o n funpassing ( f ) ; fmt . Printf ( %d %d \ n , f ( 5 ) , funproducer ( 1 0 0 ) ( 5 ) ) ; v a r funMap = map [ s t r i n g ] IntFun { f 1 : funproducer ( 1 ) , f 2 : funproducer ( 2 ) } ; fmt . Printf ( %d \ n , funMap [ f 1 ] ( 5 ) ) ; }

Output:
Passed : 1100 1005 105 6
28 / 41

Closures are Weak in Go


Go closures are not as strong as required by pure FP:
f u n c main ( ) { counter := 0 ; f1 := f u n c ( x i n t ) i n t { counter += x ; r e t u r n counter } ; f2 := f u n c ( y i n t ) i n t { counter += y ; r e t u r n counter } ; fmt . Printf ( %d \ n , f1 ( 1 ) ) ; fmt . Printf ( %d \ n , f2 ( 1 ) ) ; fmt . Printf ( %d \ n , f1 ( 1 ) ) ; }

Output:
1 2 3 // => no r e f e r e n t i a l transparency !

29 / 41

Concurrence
The idea: to impose a sharing model where processes do not share anything implictly (cf. Hoares CSP). Motto: Do not communicate by sharing memory; instead, share memory by communicating. A consequence: to reduce the synchronization problems (sometimes at the expense of performance). Only two basic constructs:
Goroutines are similar to threads, coroutines, processes, (Google men claimed they are suciently dierent to give them a new name) Channels: a typed FIFO-based mechanism to make goroutines communicate and synchronize.
30 / 41

Goroutines
To spawn a goroutine from a function or an expression, just prex it by go (in Limbo, it was spawn):
go f ( ) go f u n c ( ) { . . . } // c a l l w i t h an anonymous f u n c t i o n go list . Sort ( )

Goroutines are then automatically mapped to the OS host concurrency primitives (e.g. POSIX threads). NB: A goroutine does not return anything (side-eects are needed)

31 / 41

Channels

Basics operation on channels:


ch := make ( chan i n t ) // i n i t i a l i z a t i o n / S e n d i n g a v a l u e t o a c h a n n e l , b l o c k e d u n t i l ch < 333 / R e a d i n g a v a l u e , b l o c k e d u n t i l a v a l u e i s i := < ch it i s r e a d /

a v a i l a b l e /

Implicit synchronization through the channel semantics.

32 / 41

Concurrent Programming
Example of a simple goroutine without any synchronization (NB: recall that a goroutine cannot return a value):
v a r res i n t = 0 ; f u n c comput ( from i n t , to i n t ) i n t { tmp := 0 f o r i := from ; i < to ; i++ { tmp += i } res = tmp } f u n c main ( ) { go comput ( 0 , 1 0 0 0 0 0 0 ) ; time . Sleep ( 1 0 0 0 0 0 0 ) ; fmt . Println ( The r e s u l t : , res ) }

(without time.Sleep, the printed result will be 0...).


33 / 41

Concurrent Programming
Example of a simple goroutine with synchronization (using anonymous functions is the idiomatic way of calling expressions as a goroutine):
f u n c comput ( from i n t , to i n t ) i n t { tmp := 0 f o r i := from ; i < to ; i++ { tmp += i } r e t u r n tmp } f u n c main ( ) { ch := make ( chan i n t ) go f u n c ( ) { ch < comput ( 0 , 1 0 0 0 0 0 0 ) } ( ) res := < ch // b l o c k e d u n t i l t h e c a l c u l a t i o n fmt . Println ( The r e s u l t : , res ) }

i s done

34 / 41

Concurrent Programming
A producer/consumer agreement:
v a r ch chan i n t = make ( chan i n t ) ; f u n c prod ( ) { counter := 0 ; for { fmt . Println ( p r o d u c e d : , counter ) ch < counter ; counter++ } } f u n c consum ( ) { for { res := < ch ; fmt . Println ( consumed : , res ) ; } } f u n c main ( ) { wait := make ( chan i n t ) go prod ( ) ; go consum ( ) ; < wait // w a i t t o i n f i n i t y . . . }
(inspired from www.cowlark.com)

35 / 41

Channel Behavior Customization


Customizing the blocking behavior is possible. In particular: By specifying a FIFO buer size, that is, by using a buered channel:
Sending data to such a channel blocks if the buer is full. Reading from such a channel will not block unless the buer is empty.

Declaration example:
ch := make ( chan i n t , 1 0 0 )

36 / 41

Parallelization with Channels


Computations with some parallelization:
c o n s t NCPU =4 v a r ch chan i n t = make ( chan i n t , NCPU ) f u n c comput ( from i n t , to i n t , ch chan i n t ) { tmp := 0 f o r i := from ; i < to ; i++ { tmp += i } ch < tmp } f u n c main ( ) { iterat := 1 0 0 0 0 0 0 0 ; f o r i := 0 ; i < NCPU ; i++ { go comput ( i iterat / NCPU , ( i +1) iterat / NCPU , ch ) } tmp := 0 ; f o r i : = 0 ; i < NCPU ; i++ { tmp += < ch ; } fmt . Println ( The r e s u l t : , tmp ) }
37 / 41 (inspired from Eective Go)

Goroutines and FP
Very naturally comes the idea of mixing multi-threading and functional programming (use of anonymous functions). At least two diculties in doing that in Go:
Goroutines do not return any result ( side-eects are needed). Closures are weak.

For instance, the following variation of the preceding example does not work:
f o r i := 0 ; i < NCPU ; i++ { go f u n c ( ) { ch < comput ( i iterat / NCPU , ( i +1) iterat / NCPU ) }() }

(i is shared by all the processes...)


38 / 41

A Partial Presentation...
Go has other points of interest which could have been presented and/or more discussed here: Reection. The standard library. Memory management and pointers. Executable building. Error system (e.g., functions panic/recover, package unsafe, etc.),

39 / 41

Next Time...
The subject of the talk will be...: D is a systems programming language. Its focus is on combining the power and high performance of C and C++ with the programmer productivity of modern languages like Ruby and Python.[..!?] The D language is statically typed and compiles directly to machine code. Its multiparadigm, supporting many programming styles: imperative, object oriented, and metaprogramming. Its a member of the C syntax family, and its appearance is very similar to that of C++.[..!?] It adds to the functionality of C++ by also implementing design by contract, unit testing, true modules, garbage collection, rst class arrays, associative arrays, dynamic arrays, array slicing, nested functions, inner classes, closures, anonymous functions, compile time function execution, lazy evaluation and has a reengineered [..!?]
40 / 41

Go Installation
Go is quite easy to install (be careful about the four environment variables GOROOT, GOARCH, GOOS, GOBIN). Fully available for Linux, FreeBSD, Darwin. (NaCl : on its way) (Windows : on its way + partial Cygwin distribution). Needed : Mercurial (the VCS chosen by Go). To make a basic Go program be useful:
Source code le extension is .go. Compilation with 8g object le with extension .8. Linking with 8l exec le 8.out. Replace the above 8s by 6s for 64 bits architectures.
41 / 41

You might also like