Introduction to Programming in Go
-
Upload
amr-hassan -
Category
Software
-
view
61 -
download
3
Transcript of Introduction to Programming in Go
![Page 1: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/1.jpg)
Introduction to Programming in GoAmr Hassan
![Page 2: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/2.jpg)
Apologies
This might feel very rudimentary to you.
But this is an "introduction" after all.
![Page 3: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/3.jpg)
In the beginning... Go was created by those guys
![Page 4: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/4.jpg)
Original intentions
Go has always been intended as a systems programming language (way at the bottom of the applicationdevelopment stack).
In systems programming you would usually use C/C++ or Python if performance is not an issue for you asproductivity is.
![Page 5: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/5.jpg)
Original intentions
Born out of mutual hatred towards writing and compiling C++ code.
To the surprise of the creators, Go has seen a much higher rate adoption from the scripting crowd(Python and Ruby), and most of the C/C++ people perceived the simplicity of the langauge as a step back.
![Page 6: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/6.jpg)
Where it is now
Seemingly everywhere
Go is 5 years old now.
To the surprise of its creators, it has grown tremendously in popularity.
It's the new go-to platform for building cloud infrastructure.
![Page 7: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/7.jpg)
Where it is now
List of Go users (https://github.com/golang/go/wiki/GoUsers)
![Page 8: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/8.jpg)
What Go isn't
![Page 9: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/9.jpg)
What Go isn't
Let's start by dialing down your expectations to a manageable level.
![Page 10: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/10.jpg)
Go isn't a functional programming langauge
Although functions are first class objects
Go's adopted style is imperative through and through (sort of like C).
Your code is executed for its side effects, and you reason about your logic as sequential commandsexecuted to mutate some value within your scope of action.
Purely functional idioms in Go are really counter-productive.
So if you're a functional programming hipster, get over it.
![Page 11: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/11.jpg)
Go isn't an object-oriented language
Although Go has objects.. with methods
And a limited way of doing inheritance (it's actually called composition)
It's not OOP-driven, and won't scratch your pure OOP itch.
Trying to construct rich type hierarchies like in Java or Python is very counter-productive.
![Page 12: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/12.jpg)
Go isn't a high-level language
You might think that's implicit with "systems language", but it isn't. (cough) (Rust) (cough)
Go favors readability above all.
Go Offers just enough syntactic sugar to be productive, but not too much.
That is why a lot of the times verbosity and code simplicity are favored over complex succinctness.
![Page 13: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/13.jpg)
What Go is
![Page 14: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/14.jpg)
What Go is
Hopefully I haven't lost you yet.
![Page 15: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/15.jpg)
Go is Compiled
Compiles to native hardware instructions that can run on the bear metal.
No overhead of a VM or an interpreter.
Comes at the price of doing cross-compilation when you need multiple OS support.
Don't worry, most of the stdlib is cross-compatible.
![Page 16: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/16.jpg)
Go executables are statically-linked
Storage is cheap!
External dependencies and shared binaries are not worth the effort.
![Page 17: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/17.jpg)
Go executables are statically-linked
Very attractive feature right now.
![Page 18: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/18.jpg)
Go is statically-typed (awesomeness)
As you may know, I’m a bit biased towards static typing.
I just think it’s a really neat idea as it makes my life a lot easier.
![Page 19: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/19.jpg)
Go is statically-typed
What is static typing anyway?
Unfortunately static typing has always had this stigma of it being too verbose.
This is not the case anymore in modern languages like Go, because of type inference.
So you kind of get the best of both worlds there.
![Page 20: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/20.jpg)
Go is memory-managed
Go runtime is responsible for allocating memory for your objects.
You never need to worry about the correct (or incorrect) number of bytes to allocate.
Heap, stack or data segments are all fine with you.
![Page 21: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/21.jpg)
Go is memory-managed
Go has a tracing garbage collector.
You never need to worry about who will deallocate your memory when.
You just use objects, pass them around and then stop using them.
![Page 22: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/22.jpg)
Go is memory-managed
Unlike many other memory-managed programming languges, Go has memory pointers.
But since pointer arithmetic is unallowed (easily), it's considered safe.
Go is pragmatic
Built by software engineers (not academics) out of specific programming use cases, not in a "lab" or bylanguage designers.
Covers most of the areas that the creators felt needed addressing in their day-to-day programmingdendeavours.
![Page 23: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/23.jpg)
Go is minimalistic
Small set of orthogonal features that provide a a sweet spot balance between programmer productivityand simple software design.
Implies:- Gentle learning curve- Human-readable source code
It’s very common for people to pick up the language in an afternoon and start building thingsimmediately
Huge plus!
![Page 24: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/24.jpg)
Go is modern
As a fairly-new language, the stdlibs come with packages for doing many of the essential jobs that aprogrammer in this day would most likely require in her job.
Integrated web development stack and testing tools in the std libs and toolachain.
So no excuse to not be test-driven!
![Page 25: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/25.jpg)
Go is concurrent
Dubbed a "concurrent language" because of support for native building blocks of concurrency built intothe syntax
Go's concurrency building blocks allow you to- write and execute code concurrently directly from the language syntax- establish managed (and safe) ways for communicating between concurrently executing code
![Page 26: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/26.jpg)
Go is concurrent
The Go runtime will multiplex your concurrent code for you on actual OS threads, free of charge.
![Page 27: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/27.jpg)
Go is a bit frustrating at first
First impressions of Go is that it's very lacking.
Go is almost never anyone’s first programming language, so everyone comes to it with expectations andbiases about how things should be done according to their system of belief and the kind of programmingidiom they are used to.
Since that Go was built out of frustration with the current state of things, it's meant to disrupt the way youprogram.
![Page 28: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/28.jpg)
Go is a bit frustrating at first
You shouldn't give into that initial frustration.
Because it gets rewarding very quickly.
![Page 29: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/29.jpg)
Syntax
![Page 30: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/30.jpg)
Syntax
Most of these examples are derived from the official tour of Go on golang.org.
So if they seem too familiar, that’s probably the reason.
![Page 31: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/31.jpg)
Syntax: Hello World
Execution starts in the main() in the "main" package.
UTF-8 source code files. No special headers. No literal escaping.
You can even declare unicode symbols.Ω(DoSomething()).Should(Equal("foo"))
You probably shouldn't do that. Nice to know that you can though.
"fmt" is where you find string formatting functions.
"fmt" name is Rob Pike's doing.
package main
import "fmt"
func main() { fmt.Println("Hello, بشريا ")} Run
![Page 32: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/32.jpg)
Syntax: Variables
Variables can be declared in function scope of package scope.
package main
import "fmt"
var c, python, java bool
func main() { var i int fmt.Println(i, c, python, java)} Run
![Page 33: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/33.jpg)
Syntax: Variables with initializers
Type inference in action!
package main
import "fmt"
var i, j int = 1, 2
func main() { var c, python, java = true, false, "no!" fmt.Println(i, j, c, python, java)} Run
![Page 34: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/34.jpg)
Syntax: Shorthand variable declaration
Only inside a function body.
Both declarations are identical.
package main
import "fmt"
func main() { var i, j int = 1, 2 k := 3 c, python, java := true, false, "no!"
fmt.Println(i, j, k, c, python, java)} Run
![Page 35: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/35.jpg)
Syntax: Primitive types
bool
string
int int8 int16 int32 int64uint uint8 uint16 uint32 uint64 uintptr
float32 float64
complex64 complex128
and
byte // alias for uint8
rune // alias for int32 and it represents a Unicode code point
Zero values for booleans is false and for numerical types an actual 0.
![Page 36: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/36.jpg)
Syntax: Primitive types in action
package main
import ( "fmt" "math/cmplx")
var ( ToBe bool = false MaxInt uint64 = 1<<64 - 1 z complex128 = cmplx.Sqrt(-5 + 12i))
func main() { const f = "%T(%v)\n" fmt.Printf(f, ToBe, ToBe) fmt.Printf(f, MaxInt, MaxInt) fmt.Printf(f, z, z)} Run
![Page 37: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/37.jpg)
Syntax: Type Conversion
Unlike in a lot of other languages where automatic conversion to types with wider percision is allowed,type conversions in Go must be explicit.
The expression T(v) converts the value v to the type T.
package main
import ( "fmt" "math")
func main() { var x, y int = 3, 4 var f float64 = math.Sqrt(float64(x*x + y*y)) var z int = int(f) fmt.Println(x, y, z)} Run
![Page 38: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/38.jpg)
Syntax: Loops
![Page 39: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/39.jpg)
Syntax: Loops
Your for-s, foreach-es, and your while-s.
![Page 40: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/40.jpg)
Syntax: Loops - For Loop
Very C.
Parens are not allowed. Not even optional!
Body brakcets are obligatory.
package main
import "fmt"
func main() { sum := 0 for i := 0; i < 10; i++ { sum += i } fmt.Println(sum)} Run
![Page 41: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/41.jpg)
Syntax: Loops - For Loop
package main
import "fmt"
func main() { sum := 1 for ; sum < 1000; { sum += sum } fmt.Println(sum)} Run
![Page 42: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/42.jpg)
Syntax: Loops - While Loop
package main
import "fmt"
func main() { sum := 1 for sum < 1000 { sum += sum } fmt.Println(sum)} Run
![Page 43: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/43.jpg)
Syntax: While (TRUE) Loop
package main
import ( "fmt" "time")
func main() { for { fmt.Println("Tick!") time.Sleep(1000) fmt.Println("Tock!") time.Sleep(1000) }} Run
![Page 44: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/44.jpg)
Syntax: Conditionals - If
Also very C.
Also body brakcets are obligatory
Again, parens are not even optional.
package main
import ( "fmt" "math")
func sqrt(x float64) string { if x < 0 { return sqrt(-x) + "i" } return fmt.Sprint(math.Sqrt(x))}
func main() { fmt.Println(sqrt(2), sqrt(-4))} Run
![Page 45: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/45.jpg)
Syntax: Conditionals - If with pre-statement
package main
import ( "fmt" "math")
func pow(x, n, lim float64) float64 { if v := math.Pow(x, n); v < lim { return v } return lim}
func main() { fmt.Println( pow(3, 2, 10), pow(3, 3, 20), )} Run
![Page 46: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/46.jpg)
Syntax: Conditionals - If/else
package main
import ( "fmt" "math")
func pow(x, n, lim float64) float64 { if v := math.Pow(x, n); v < lim { return v } else { fmt.Printf("%g >= %g\n", v, lim) } // can't use v here, though return lim}
func main() { fmt.Println( pow(3, 2, 10), pow(3, 3, 20), )} Run
![Page 47: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/47.jpg)
Syntax: Conditionals - Switch
Switch is a fancy if with multiple if/else clauses.
package main
import ( "fmt" "runtime")
func main() { fmt.Print("Go runs on ") switch os := runtime.GOOS; os { case "darwin": fmt.Println("OS X.") case "linux": fmt.Println("Linux.") default: // freebsd, openbsd, // plan9, windows... fmt.Printf("%s.", os) }} Run
![Page 48: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/48.jpg)
Syntax: Conditionals - Switch case evaluation
switch i {case 0:case f():}
The evaluation order of the switch case statements happens from top to bottom.
![Page 49: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/49.jpg)
Syntax: Functions
The main building block in Go.
You can create nested closures using the func keyword.
package main
import "fmt"
func add(a int, b int) int { return a + b}
func main() { sum := add(3, 1) fmt.Println("Initial sum", sum)
// Closures! and first-class function values incrementSum := func() { sum += 1 }
incrementSum() incrementSum() fmt.Println("Incremented sum", sum)} Run
![Page 50: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/50.jpg)
Syntax: Functions
The type of function values is:
func([argument_list]) [return_type]
![Page 51: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/51.jpg)
Syntax: Higher order functions
We can use function types to declare some higher order functions like
package mainimport "fmt"
func add(a int, b int) int { return a + b }func subtract(a, b int) int { return a - b }
func execute(a, b int, operation func(int, int) int) int { // shorthand for duplicate types return operation(a, b)}
func main() { i, j := 4, 2
added := execute(i, j, add) fmt.Printf("Added %d + %d == %d\n", i, j, added)
subtracted := execute(i, j, subtract) fmt.Printf("Subtracted %d - %d == %d\n", i, j, subtracted)} Run
![Page 52: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/52.jpg)
Syntax: Defer
An awesome Go-first!
Push clean-up code to be executed before the function returns in LIFO.
Go's way of making up for not managing resources for you other than memory and CPU.
package main
import "fmt"
func main() { defer fmt.Println("world")
fmt.Println("hello")} Run
![Page 53: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/53.jpg)
Syntax: Stacking defer statements
package main
import "fmt"
func main() { fmt.Println("counting")
for i := 0; i < 10; i++ { defer fmt.Println(i) }
fmt.Println("done")} Run
![Page 54: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/54.jpg)
Syntax: Pointers
A Go pointer value is nothing but a typed memory addresses, much like a C pointer.
*T is a pointer to a T value
The zero value of a pointer is nil (no garbage values in Go).
![Page 55: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/55.jpg)
Syntax: Pointers
This is how you declare a pointer.
var p *int
The & operator generates a pointer to its operand.
i := 42p = &i
The * operator denotes the pointer's underlying value.
fmt.Println(*p) // read i through the pointer p
Dereferencing a pointer is also via the * operator.
*p = 21 // set i through the pointer p
Unlike C, Go has no pointer arithmetic.
![Page 56: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/56.jpg)
Syntax: Structs
The the way of building heterogeneous aggregate custom types in Go.
That's fancy talk for saying it's a collection of fields of different types.
package main
import "fmt"
type Vertex struct { X int Y int}
func main() { fmt.Println(Vertex{1, 2})} Run
![Page 57: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/57.jpg)
Syntax: Structs
Struct fields are accessible using the dot notation
package main
import "fmt"
type Vertex struct { X int Y int}
func main() { v := Vertex{1, 2} v.X = 4 fmt.Println(v.X)} Run
![Page 58: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/58.jpg)
Syntax: Struct literal notation
package main
import "fmt"
type Vertex struct { X, Y int}
var ( v1 = Vertex{1, 2} // has type Vertex v2 = Vertex{X: 1} // Y:0 is implicit v3 = Vertex{} // X:0 and Y:0 p = &Vertex{1, 2} // has type *Vertex)
func main() { fmt.Println(v1, p, v2, v3)} Run
![Page 59: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/59.jpg)
Syntax: Pointers to structs
Quickly instantiate and populate with values a struct all at once
package main
import "fmt"
type Vertex struct { X int Y int}
func main() { v := Vertex{1, 2} p := &v p.X = 1e9 fmt.Println(v)} Run
![Page 60: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/60.jpg)
Syntax: Structs are your main objects
If you’re coming from a strictly OOP language like Java, the only thing you would be thinking about in yourprogram design, is classes.
Classes this, classes that, classes everywhere.
![Page 61: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/61.jpg)
Syntax: Structs are your main objects
Go creators did not like that approach where everything had to be a class.
Sometimes, the simplest most correct way to express your computation, is just as a function.
Go fell back to the minimalistic approach of C, where your data structures are just pure aggregate data.
And you can as an added bonus, you can specify type-specific operations on each of your custom types.
It’s like how in C you would declare struct s and then declare functions that all accept a pointer to thestruct as their first argument by convention.
![Page 62: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/62.jpg)
Syntax: Struct methods
Two argument lists. First one only has the "receiver" argument. Second one has zero or more argumetnsof your method.
Dot notation for invoking struct methods on the receiver.
package mainimport ("fmt"; "math")
type Vector struct { X float64 Y float64}
func (v Vector) Magnitude() float64 { return math.Sqrt(v.X*v.X + v.Y*v.Y) }
func (v *Vector) Add(other *Vector) *Vector { return &Vector{X: v.X + other.X, Y: v.Y + other.Y} }
func main() { vec := Vector{3.0, 4.0} fmt.Println(vec) fmt.Println(vec.Magnitude()) fmt.Println(vec.Add(&Vector{6.0, 4.0}))} Run
![Page 63: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/63.jpg)
Syntax: Arrays
The way of constructing homogeneous aggregate types in Go (a sequence of values of the same type).
Analogous to boring C arrays.
Fixed in size.
Size is part of the type.
package main
import "fmt"
func main() { var a [2]string a[0] = "Hello" a[1] = "World" fmt.Println(a[0], a[1]) fmt.Println(a)} Run
![Page 64: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/64.jpg)
Syntax: Slices
Slices are arrays on steroids.
Go makes up for the fact that it doesn’t let you do pointer arithmetic, by giving you this much cooler andeasier to use concept of slices.
A slice is more or less a view into a subset of an array.
[]T is a slice with elements of type T.
A slice's size is not reflected in its type.
![Page 65: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/65.jpg)
Syntax: Slices
The slice literal notation lets you create slices backed by anonymous arrays very easily and populate themwith values on the spot.
Slices are used just like you would use an array, except for a couple of extra very neat features.
package main
import "fmt"
func main() { p := []int{2, 3, 5, 7, 11, 13} fmt.Println("p ==", p)
for i := 0; i < len(p); i++ { fmt.Printf("p[%d] == %d\n", i, p[i]) }} Run
![Page 66: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/66.jpg)
Syntax: Slicing slices
You can slice slices!
Very similar to sequence slicing in Python (except that negative indices are not supported).
You can slice into a slice to get a new view into its backing array, or you can slice into an already existingarray to get a slice from that.
package mainimport "fmt"
func main() { p := []int{2, 3, 5, 7, 11, 13} fmt.Println("p ==", p) fmt.Println("p[1:4] ==", p[1:4])
// missing low index implies 0 fmt.Println("p[:3] ==", p[:3])
// missing high index implies len(s) fmt.Println("p[4:] ==", p[4:])} Run
![Page 67: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/67.jpg)
Syntax: Slicing slices
The zero value of a slice is nil.
![Page 68: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/68.jpg)
Syntax: Appending to slices
package mainimport "fmt"
func main() { var a []int printSlice("a", a)
// append works on nil slices. a = append(a, 0) printSlice("a", a)
// the slice grows as needed. a = append(a, 1) printSlice("a", a)
// we can add more than one element at a time. a = append(a, 2, 3, 4) printSlice("a", a)}
func printSlice(s string, x []int) { fmt.Printf("%s len=%d cap=%d %v\n", s, len(x), cap(x), x)} Run
![Page 69: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/69.jpg)
Syntax: Appending to slices
If the backing array of the slice is too short, then append() would create a new backing array big enoughand use that in the returned slice.
![Page 70: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/70.jpg)
Syntax: Maps
The native type-safe associative array in Go is the hash map, almost identical to the native map type inPython.
![Page 71: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/71.jpg)
Syntax: Maps
You must create a map using the make() function before usage, because the zero value of map is nil andusing that as a map causes in an error.
You can create maps from any type to any type.
package main
import "fmt"
type Vertex struct { Lat, Long float64}
var m map[string]Vertex
func main() { m = make(map[string]Vertex) m["Bell Labs"] = Vertex{ 40.68433, -74.39967, } fmt.Println(m["Bell Labs"])} Run
![Page 72: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/72.jpg)
Syntax: Map Literals
package main
import "fmt"
type Vertex struct { Lat, Long float64}
var m = map[string]Vertex{ "Bell Labs": Vertex{ 40.68433, -74.39967, }, "Google": Vertex{ 37.42202, -122.08408, },}
func main() { fmt.Println(m)} Run
![Page 73: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/73.jpg)
Syntax: Map operations
package main
import "fmt"
func main() { m := make(map[string]int)
m["Answer"] = 42 fmt.Println("The value:", m["Answer"])
m["Answer"] = 48 fmt.Println("The value:", m["Answer"])
delete(m, "Answer") fmt.Println("The value:", m["Answer"])
v, exists := m["Answer"] fmt.Println("The value:", v, "Present?", exists)} Run
![Page 74: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/74.jpg)
Syntax: Range loops
21st century looping in Go
Similar to Python sequence looping
for i, v in enumerate([1, 2, 3])
and map looping
for k, v in mapping.items()
package main
import "fmt"
var pow = []int{1, 2, 4, 8, 16, 32, 64, 128}
func main() { for i, v := range pow { fmt.Printf("2**%d = %d\n", i, v) }} Run
![Page 75: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/75.jpg)
The Zen of Go
![Page 76: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/76.jpg)
The Zen of Go: Interfaces
Go resists this:- subtype polymorphism (inheritance).- parametric-type polymorphism (generics).
It instead emphasizes polymorphism via interfaces.
![Page 77: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/77.jpg)
The Zen of Go: Interfaces
Go interfaces are small.
type Stringer interface { String() string}
A Stringer can pretty print itself.Anything that implements String is a Stringer.
Interfaces are implemented implicitly.
![Page 78: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/78.jpg)
The Zen of Go: Interfaces
Interfaces are types.
package mainimport "fmt"
type Named interface { Name() string}
func greet(someone Named) { fmt.Println("Greetings, " + someone.Name()) }
type Human struct { firstName string}
func (h Human) Name() string { return h.firstName}
func main() { greet(Human{firstName: "Jack Bauer"})} Run
![Page 79: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/79.jpg)
The Zen of Go: Interfaces
A Sorting example
package mainimport ("fmt"; "sort")
type Person struct { Name string Age int}func (p Person) String() string { return fmt.Sprintf("%s: %d", p.Name, p.Age) }
// ByAge implements sort.Interface for []Person based on// the Age field.type ByAge []Personfunc (a ByAge) Len() int { return len(a) }func (a ByAge) Swap(i, j int) { a[i], a[j] = a[j], a[i] }func (a ByAge) Less(i, j int) bool { return a[i].Age < a[j].Age }
func main() { people := []Person{{"Bob", 31}, {"John", 42}, {"Michael", 17}, {"Jenny", 26}} fmt.Println(people) sort.Sort(ByAge(people)) fmt.Println(people)} Run
![Page 80: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/80.jpg)
The Zen of Go: Error handling
![Page 81: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/81.jpg)
The Zen of Go: Error handling
Unchecked exceptions are evil.
This is not scripting. You're building "robust" infrastructure software.
"Let it crash" cannot be the default way of handling errors.
Every computation that can fail must be executed with proper "error handling code".
![Page 82: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/82.jpg)
The Zen of Go: Error handling
Memorize this pattern. You're gonna use it so much it'll haunt you in your dreams.
package main
import ( "fmt")
func getMyText() (string, error) { // This could have been read from disk or a network socket return "Your text", nil}
func main() { myText, err := getMyText() if err != nil { myText = "Sorry, I couldn't get your text. May I interest you in a joke?" }
fmt.Println(myText)} Run
![Page 83: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/83.jpg)
The Zen of Go: Error handling
Go chose simplest approach possible to error handling.
Errors are plain regular values that implement the error interface.
type error { Error() string}
You should either handle an error or propagate it upwards.
![Page 84: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/84.jpg)
The Zen of Go: Error handling
Unexpected errors should not be expected
package main
import ("fmt"; "regexp")
func extractLongestNumber(text string) string { extractorRegexp, err := regexp.Compile("([0-9]+)") if err != nil { panic(err) // This will crash with a stack trace and the value of err }
matches := extractorRegexp.FindStringSubmatch(text) if len(matches) > 1 { return matches[1] } else { return "" }}
func main() { myText := "Sonmi-451" fmt.Println(extractLongestNumber(myText))} Run
![Page 85: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/85.jpg)
The Zen of Go: Error handling
This is so common that there's a pattern for it.
package main
import ("fmt"; "regexp")
func extractLongestNumber(text string) string { extractorRegexp := regexp.MustCompile("([0-9]+)") matches := extractorRegexp.FindStringSubmatch(text) if len(matches) > 1 { return matches[1] } else { return "" }}
func main() { myText := "Sonmi-451" fmt.Println(extractLongestNumber(myText))} Run
![Page 86: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/86.jpg)
The Zen of Go: Error handling
Having to handle every each possible error case manually is a common first-day complaint in Go
You get used to it very quickly.
You will definitely feel its reward when you run your code and find it correct and not crashing the firsttime.
![Page 87: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/87.jpg)
The Zen of Go: Packages
![Page 88: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/88.jpg)
The Zen of Go: Packages
Packages are one more thing that newcomers clash against.
Go comes with its own way of doing packages and file hierarchy for source code.
Just stop fighting it.
![Page 89: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/89.jpg)
The Zen of Go: Packages
The first thing you'll do after installing the Go SDK is setting up your $GOPATH.
$GOPATH is structured like so:
|- src|- bin|- pkg
The import path of your packages is going to be the relative path under your $GOPATH/src directory.
Doing
import "coderize/awesomelib"
imports the package from $GOPATH/src/coderize/awesomelib as the awesomelib package
It's as simple as that. Don't invent your own system.
![Page 90: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/90.jpg)
The Zen of Go: Visibility
Code visibility is only on the package level.
Only symbols starting with an upper class letter are exported.
package awesomelib
type ExportedType interface { PublicAction()}
type internalType struct { internalNumber int}
func (a internalType) PublicAction() { /* I work in silence*/ }
func internalDityWork() { /* TMI*/ }
func NewAwesomeness() ExportedType { /* TMI*/ return internalType{internalNumber: 42} }
![Page 91: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/91.jpg)
The Zen of Go: Documentation
![Page 92: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/92.jpg)
The Zen of Go: Documentation
No special syntax.
Code documentation should be readable to humans in source code text format.
// Accepts an io.Reader and does the agreed-upon computation and returns the result// or a descriptive error if something went wrong.func ExportedFunction(reader io.Reader) (Result, error)
No special markup tags to describe parameters or return types.
If you make sure all your exported symbols are documented, Godoc can render that into perfectlypresentable HTML for you.
godoc (http://godoc.org/)
![Page 93: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/93.jpg)
The Zen of Go: Concurrency
![Page 94: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/94.jpg)
The Zen of Go: Concurrency
I've saved the best for last.
Concurrency is a session topic on its own.
Let's just skim over of what Go has to offer.
![Page 95: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/95.jpg)
The Zen of Go: Concurrency
A goroutine is a lightweight thread managed by the Go runtime.
Go routines are multiplexed over multiple actual OS threads by the runtime.
go f(x, y, z)
starts a new goroutine running
f(x, y, z)
![Page 96: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/96.jpg)
The Zen of Go: Concurrency
package main
import ( "fmt" "time")
func say(s string) { for i := 0; i < 5; i++ { time.Sleep(100 * time.Millisecond) fmt.Println(s) }}
func main() { go say("world") say("hello")} Run
![Page 97: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/97.jpg)
The Zen of Go: Concurrency
A channel is a typed synchronized queue.
boolChannel := make(chan bool) // I created a boolean channel of type "chan bool"
Channels are utilized for communication and synchronization between running goroutines.
There are three things you can do on a channel: send stuff, receive stuff, or close the channel.
boolChan <- = falseboolChan <- = trueboolChan <- = true
close(boolChan)
I just sent a "false" and two "true"s to the channel then closed it.
![Page 98: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/98.jpg)
The Zen of Go: Concurrency
On the receiving end:
// Listens on the given channel for three incoming boolean messages and returns true if any one// was truefunc getMyBooleans(myChannel chan bool) bool { firstMessage, isOpen := <- boolChan if !isOpen { return false } secondMessage, isOpen := <- boolChan if !isOpen { return firstMessage } thirdMessage, isOpen := <- boolChan if !isOpen { return firstMessage | secondMessage } return firstMessage | secondMessage | thirdMessage}
![Page 99: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/99.jpg)
The Zen of Go: Concurrency
Too verbose? range to the rescue:
// Listens on the given channel for incoming boolean messages and returns true if any one// was truefunc getMyBooleans(myChannel chan bool) bool { message := false for incomingMessage := myChannel { message |= incomingMessage } return message}
![Page 100: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/100.jpg)
The Zen of Go: Concurrency
Channel sends and receives are blocking operations. So they're perfect for doing "lock-free"synchronization.
![Page 101: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/101.jpg)
The Zen of Go: Concurrency
Here's a really dumb example that takes too much time to compute
package mainimport "fmt"
func fibonacci(i uint) uint { if i <= 1 { return 1 } return fibonacci(i-1) + fibonacci(i-2)}
func main() { fmt.Println(fibonacci(41)) fmt.Println(fibonacci(41))} Run
![Page 102: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/102.jpg)
The Zen of Go: Concurrency
Same example but now made faster using parallelism via goroutines and channels.
package mainimport "fmt"
func compute(computation func() int) <- chan int { outputChannel := make(chan int) go func() { outputChannel <- computation() }() return outputChannel}
func fibonacci(i int) int { if i <= 1 { return 1 } else {return fibonacci(i-1) + fibonacci(i-2) }}
func main() { computation := func() int {return fibonacci(41)} firstResult := compute(computation) secondResult := compute(computation) // Now I'm gonna block and wait for the two results fmt.Println(<- firstResult) fmt.Println(<- secondResult)} Run
![Page 103: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/103.jpg)
Where to Go from here (pun intended)
![Page 104: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/104.jpg)
Where to Go from here (pun intended)
There are still several things that I did not have the time to cover in Go.
I suggest checking out the links below.
Take the Go tour yourself (http://tour.golang.org/)
Go for Gophers (by Andrew Gerrand) (https://talks.golang.org/2014/go4gophers.slide)
Go Concurrency Patterns (by the awesome Rob Pike) (https://www.youtube.com/watch?feature=player_detailpage&v=f6kdp27TYZs)
Go subreddit (http://www.reddit.com/r/golang)
![Page 105: Introduction to Programming in Go](https://reader034.fdocuments.us/reader034/viewer/2022042717/55d6b109bb61ebc7338b47be/html5/thumbnails/105.jpg)
Thank you
Amr Hassan