18 Golang Overview
18 Golang Overview
e An overview of Go E
1 Overview
2 Language Syntax
3 Closure
5 Co-routines
“
Go is an attempt to combine the
safety and performance of stat-
ically typed languages with the
convenience and fun of dynam-
ically typed interpretative lan-
guages.
–
Rob Pike
1 Overview
2 Language Syntax
3 Closure
5 Co-routines
package main
import (
"fmt"
"os"
)
func main() {
fmt.Println("Hello ",
os.Args[1])
}
Documentation:
godoc -http=":6060"
http://localhost:6060/pkg/
import "fmt"
import "math/rand"
func main() {
fmt.Println("My favorite",
" number is",
rand.Intn(10))
}
import "fmt"
import "math"
func main() {
fmt.Println(math.Pi)
}
Variables declared without an explicit initial value are given their zero value (for
string ””, for bool false, …)
The expression T(v) converts the value v to the type T
func main() {
var i int = 42
var f float64 = float64(i)
var b bool
var s string
fmt.Printf("%v %v %v %q\n", i, f, b, s)
}
func main() {
fmt.Println(Small)
fmt.Println(Small*2.01)
}
Typology of programming languages An overview of Go 15 / 61
For init;condition; loop { body }
No parentheses surrounding the
three components of the for
statement
The braces are always required.
The loop will stop iterating once the
boolean condition evaluates to false.
The init and post statement are
optional (while loop)
Omit the loop condition to get a
forever loop
for i := 0; i < 9; i++ {
...
}
* allows dereferences
& generates a pointer to its operand
No pointer arithmetic
func main() {
var i int = 21
var p* int = &i
fmt.Println(*p)
*p = *p + 2
fmt.Println(i)
}
1 Overview
2 Language Syntax
3 Closure
5 Co-routines
func main () {
counter := 0;
f1 := func (x int) int {
counter += x; return counter
}
f2 := func (y int) int{
counter += y; return counter
}
fmt.Printf(" %d \n", f1(1))
fmt.Printf(" %d \n", f2(1))
fmt.Printf(" %d \n", f1(1))
}
1 Overview
2 Language Syntax
3 Closure
5 Co-routines
type My struct {
X, Y float64
}
func (v* My) SetX(x float64) {
v.X = x
}
func main() {
v := My{3, 4}
v.SetX(18)
}
Typology of programming languages An overview of Go 34 / 61
Functions associated to a type 3/3
We can declare a function on
non-struct types
Possible, only for function with a
receiver whose type is defined in the
same package as the function
type My float64
func (f My) Abs() float64 {
if f < 0 {
return float64(-f)
}
return float64(f)
}
func main() {
f := My(-math.Sqrt2)
fmt.Println(f.Abs())
}
Typology of programming languages An overview of Go 35 / 61
Interface
An interface type is defined as a set
of method signature
A value of interface type can hold
any value that implements it
type Runner interface {
Run() int
}
type MyType struct {
X int
}
func (v MyType) Run() int {
return 42
}
func main() {
var a Runner; v := MyType{3}
a = v; fmt.Println(a.Run())
}
//...
fmt.Println(Person{"John Doe", 42})
func main() {
v1 := MyType1{3}
v2 := MyType2{3, 4}
fmt.Println(v1.Run(), v2.Run())
fmt.Println(run(v1),run(v2))
}
Typology of programming languages An overview of Go 38 / 61
Maximum Polymorphism and Reflection
maximum polymorphism through
the empty interface: ”interface {}”
For example, the printing functions
in fmt use it
Need for some reflection
mechanisms, i.e. ways to check at
runtime that instances satisfy types,
or are associated to functions.
For instance, to check that x0
satisfies the interface I
x1, ok := x0.(I);
Go functional polymorphism is a
type-safe realization of “duck
typing”.
Go interface-based mechanism is
not new, neither very powerful..
Haskell offers type inference with
constrained genericity, and
inheritance
Rule
If type T is compatible with FooBar, it is compatible with Foo and Bar too
1 Overview
2 Language Syntax
3 Closure
5 Co-routines
Motto
Do not communicate by sharing
memory; instead, share memory by
communicating.
Objectives
Reduce the synchronization problems
(sometimes at the expense of
performance)
Typology of programming languages An overview of Go 47 / 61
Three basic constructs
Goroutines are similar to threads,
coroutines, processes, (Googlers
claimed they are sufficiently
different to give them a new name)
I Goroutines are then automatically
mapped to the OS host
concurrency primitives (e.g. POSIX
threads)
I A goroutine does not return
anything (side-effects are needed)
Channels: a typed FIFO-based
mechanism to make goroutines
communicate and synchronize
Segmented stacks make co-routines
usables
Demo.
1 Overview
2 Language Syntax
3 Closure
5 Co-routines
package main
import (
"fmt"
"reflect"
)
import "fmt"
func main() {
fmt.Println("counting")
for i := 0; i < 10; i++ {
defer fmt.Println(i)
}
fmt.Println("done")
} // counting done 9 8 7 6 5 4 3 2 1 0
Typology of programming languages An overview of Go 58 / 61
Panic and Recover 1/2
Panic is a built-in function that
stops the ordinary flow of control
and begins panicking.
Recover is a built-in function that
regains control of a panicking
goroutine. Recover is only useful
inside deferred functions.
package main
import "fmt"
func main() {
f()
fmt.Println("Returned normally from f.")
}