SlideShare ist ein Scribd-Unternehmen logo
1 von 53
Introduction to Go
Go
• Go (or GoLang) is a general purpose programming language
created by Google.
• First version appeared at 2009.
• Some well known open-source projects using Go:
• Docker.
• Kubernetes.
• OpenShift.
Go
• Go language characteristics:
• Statically-typed.
• Compiled.
• Garbage-collected.
• Memory-safe.
Go
• There is no large runtime to install like in Java.
• Great concurrency model (goroutines & channels).
• Extremely efficient.
• Large standard library.
• AOT compilation.
• Fast compilation.
• Small and clear language spec.
• Integrates well with C.
Go vs. Java
• Java is definitely more high level.
• Provides generics and classes.
• However, comes with a large runtime (less in Java 9).
• Containers become large.
• Not suited for command line utilities.
• First interpreted then compiled.
• Large external dependencies.
Go vs. Python
• Performance, Performance, Performance.
• Python is dynamically typed.
• However, Python has its use-cases (e.g., machine learning).
Go vs. C
• Go is safer.
• Provides superior concurrency utilities.
• More productive.
strings
• Strings in Go are built-in types.
• They are not pointers (like in Java) but values.
• They are never null (or nil in Go).
• Note that strings are immutable in Go.
Arrays
• Arrays have fixed size in Go.
• Declared with [n]T.
• Where n is the size and T is the element type.
• The built-in len function returns the size of the array.
• Accessing an element is done with arr[idx].
• Index is zero-based.
Arrays
• The length of the array is part of its type.
• For example:
var arr1 [10]int
var arr2 [10]int
var arr3 [4]int
arr1 = arr2
arr2 = arr1
// Compilation error
// not of the same type
arr3 = arr2
Arrays
• Important: arrays are values. They are copied on assignment or
when sent as a function argument.
• In most cases this will result in an incorrect result or in a
performance hit.
Slices
• Usually, you’ll not work with arrays directly (due to the fact that
they are values).
• You’ll work with slices.
• A slice is a reference to an array.
• It is analogous to an array pointer.
Slices
• A slice holds a pointer inside the array, a length and the
capacity (the maximum allowed length inside the underlying
array).
• Example:
// array
var arr4 [10]int
// slice
var slice1 []int = arr4[0:2]
// prints 2
println(len(slice1))
// prints 10
println(cap(slice1))
// slice
slice2 := arr4[2:4]
// prints 2
println(len(slice2))
// prints 8
println(cap(slice2))
Slices Representation
• A slice, behind the scenes, contains a pointer into the array, a
length and capacity.
• The capacity is the maximum allowed growth of the slice (from
current location until the end of the underlying array).
• s2 = s2[:cap(s2)+1]
• Runtime error!
Working with Slices
• Usually, you create slices with the make function.
• The make function accepts a type, length and capacity.
• Go provides with 2 additional functions to work with slices:
• copy – accepts destination and source and returns the number of
elements copied (min(len(dst), len(src))).
• append – appends elements to a slice, possibly allocates a new
underlying array.
Important
• Note that the underlying array will not be collected (disposed)
as long as there is a slice pointing into it.
• May cause memory problems in some cases.
• Consider using copy/append to avoid these situations!
Ranges
• Using the range keyword you can iterate over: slices, array,
string or map.
• Produces two iteration variables:
• Arrays, slices, pointers to arrays – idx, value.
• maps – key, value.
• strings – byte index, rune.
• Can also work on channels (discussed later).
Ranges
• Important, the range expression is evaluated once.
• Constant-sized arrays will not cause any range evaluation.
• nil values will produce zero iterations!
if/else
• If statements resemble for loops (without the step clause).
• The initializer part is optional. There are no parenthesis.
• Example:
x := 5
if y := x * x; y < 20 {
println("if")
} else {
println("else")
}
if x < 10 {
println("again")
}
switch
• Go provides a switch statement.
• No fall-through unless explicitly stated.
• Example:
x := 5
switch z := x*x; z {
case 25:
println("hello")
println("world")
fallthrough
case 30:
println("false")
}
switch
• If you drop the condition, you get a concise syntax for if-else
chains.
• Due to the fact that case clauses can contain expressions.
• Example:
x := rand.Int()
switch {
case x < 10:
println("x < 10")
case x > 10:
println("x > 10")
default:
println("x == 10")
}
Structs
• A struct is a group of fields.
• General syntax: type name struct { fields }.
• Example:
type Point struct {
x int
y int
}
Structs
• Struct instances are created like the following:
var p1 Point = Point{10,20}
p2 := Point{10,20}
p3 := Point{y:20, x: 10}
Structs
• You can access a struct field by using dot (.) followed by the
field name.
• Example:
var p1 Point = Point{10,20}
// print the x+y value:
println(p1.x + p1.y)
Functions
• Function declaration in Go starts with the func keyword.
• General syntax is: func name (args) return_type { body}.
• Example:
func greaterThan10(x int) bool {
return x > 10
}
Functions
• Functions in Go can return multiple results.
• You can even name the results.
• If you name the results, it defines the variables at the beginning
of the function.
• A return without values, will automatically return those variables.
• Let’s see an example…
Multiple Results
func DivAndMod(a,b int) (q,r int) {
q = a / b
r = a % b
return
}
func main() {
q,r := DivAndMod(10,3)
// prints 3
println(q)
// prints 1
println(r)
Naming Conventions
• As you may have noticed, Go uses CamelCase notation.
• Functions that are exported (i.e., part of your public API) should
start with an uppercase letter.
• If it’s part of your internal API, it should start with a lowercase
letter.
• The same goes for variables.
Exports
• So, what is exported from your package (i.e., visible to other
packages)?
• Everything that starts with an uppercase.
• “export” is not a keyword.
Pointers
• Go has pointers.
• A pointer holds the memory address of a variable.
• Note that Go doesn’t provide pointer arithmetic like C.
• The zero value for a pointer is: nil.
Pointers
• Note that Go is a by-value language.
• It means that when an argument is passed to a function, it will
pass by value.
• I.e., its value will be copied.
• Pointers are just a memory address number.
Pointers
• Go guarantees that the object pointed by the pointer will be
”alive” and reachable as long as it is referenced.
• Due to the fact that there is no pointer arithmetic, pointers and
arrays are not equal like in C.
Pointers Syntax
• Pointer types are regular types preceded by an asterisk (*).
• Example: func f1(a int) {
a = 8
}
func f2(a *int) {
*a = 8
}
func main() {
xx := 10
f1(xx)
println(xx) // prints 10
// passing the pointer to xx
f2(&xx)
println(xx) // prints 8
Defer
• The defer keyword allows to provide some code that will be
executed when the function returns.
• When several defer statements are provided, they are executed
in a LIFO manner.
• One classic usecase is to provide resource-cleaning operations.
• Let’s see an example…
Defer
func boo() {
defer println("This line is deffered 1!")
defer println("This line is deffered 2!")
println("This line is NOT deferred!")
}
The Blank Identifier
• The blank identifier, ‘_’, is used as a write-only, immediately
discarded value.
• Let’s discuss several use-cases for it:
• Discard some values from multiple results of a function.
• Silence work-in-progress, unused imports.
• Imports for side-effects.
• Interface checks.
Maps
• One of the basic data-structures in Go is: map.
• General syntax for the type: map[keyType]valueType.
• For example, a map from int to string is the type: map[int]string.
• Note that the key type must be a comparable.
Maps
• If the key doesn’t exist, we get the zero value (pun intended).
• The built-in function len returns the number of elements in the
map.
• The delete function removes a key-value pair from the map.
• You can use two-value assignment to test if the key exists:
m := make(map[int]string)
m[1] = "hello"
s1 := m[2]
s2, ok := m[2]
// prints false
println(ok)
Comparable Types
• Comparable types in Go are:
• string, numbers, booleans, pointers, channels, interfaces.
• Structs and arrays that are composed only of these types.
• Non-comparable: functions, slices, maps.
• Comparable types can be compared using ==.
Types
• You can introduce new types with the type keyword.
• Note that two types are distinct even if they share the same
layout.
• Example:
type XY int
type ZZ int
func main() {
var uu XY = 40
var ii ZZ = 80
// doesn't compile
ii = uu
No Classes
• Go is not your classic Object Oriented programming language.
• There are no classes in Go.
• Instead it has interfaces!
Interfaces
• An interface is a collection of methods.
• Example:
type Vehicle interface {
NumberOfWheels() int
HasMotor() bool
}
Interfaces
• So, an interface provides no implementation.
• A type can implement the interface implicitly.
• For example:
type Bicycle struct {
model string
}
func (b Bicycle) NumberOfWheels() int {
return 2
}
func (b Bicycle) HasMotor() bool {
return false
}
Interfaces
• There is no classic Object-Oriented virtual table or inheritance
in go.
• Instead, if there is an implementation for the interface methods
for the type, the implementation will be invoked:
func ShowVehicle(v Vehicle) {
println(v.NumberOfWheels())
}
func main() {
bi := Bicycle{"BMX"}
ShowVehicle(bi)
Dynamic Types
• In Go, each variable has a static type and a dynamic type.
• The static type is the type stated at the declaration site.
• For non-interface types, it is also always the dynamic type.
• For interface types, the dynamic type is the actual type stored in
the variable at runtime.
Dynamic Types
• Example:
var xx int = 9
var yy interface{} = 9
// doesn't compile
xx = "hello"
// compiles
yy = "hello"
Stringer
• One of the popular interfaces in Go is Stringer.
• Defined in the fmt package.
• Comes with a single method:
• String() string.
• Types implementing this interface can be easily converted into
strings.
• Think, should there be a default implementation for this
interface (like in Java)?
Concurrency
• Concurrency in Go revolves around CSP (Communicating
Sequential Processes).
• Instead of threads, Go has goroutines.
• Very lightweight (compared to thread).
• Resemble actors (thought not the same).
Goroutines
• You have have thousands or more concurrent goroutines.
• You start a goroutine with the go keyword.
• When a coroutine “blocks” the same thread will be allocated
with another coroutine.
Channels
• Channels are the concurrency-communication mechanism in
Go.
• First class citizens.
• Built-in keyword: chan.
• Created with the make function.
• Example:
out := make(chan int)
Channels
• As you can see, channels are typed.
• You can declare directionality:
foo(x int, c chan<- int)
foo(y int, c <-chan int)
• If you don’t provide the arrow, you can both send and receive.
• Like at the creation site.
Channels vs. Actors
• Actors are like a goroutine with a single input channel.
• In go, you can read from the same channel with multiple
goroutines.
• Actor frameworks (e.g., Akka), are usually more feature-rich.
Code Generations
• Go has a lot of support for code-generation.
• Go generate is a nice utility for running our code generation tool
according to annotations.
• Used heavily in:
• Marshalling/Serialization.
• Implementing complex interfaces (with good default type-specific
implementations. E.g., Kubernetes code generation.

Weitere ähnliche Inhalte

Was ist angesagt?

From V8 to Modern Compilers
From V8 to Modern CompilersFrom V8 to Modern Compilers
From V8 to Modern CompilersMin-Yih Hsu
 
20140626 red dotrubyconf2014
20140626 red dotrubyconf201420140626 red dotrubyconf2014
20140626 red dotrubyconf2014Hiroshi SHIBATA
 
How to develop the Standard Libraries of Ruby?
How to develop the Standard Libraries of Ruby?How to develop the Standard Libraries of Ruby?
How to develop the Standard Libraries of Ruby?Hiroshi SHIBATA
 
SIL - All you need to know about Swift Intermediate Language
SIL - All you need to know about Swift Intermediate LanguageSIL - All you need to know about Swift Intermediate Language
SIL - All you need to know about Swift Intermediate LanguageBartosz Polaczyk
 
Experiences with Microservices at Tuenti
Experiences with Microservices at TuentiExperiences with Microservices at Tuenti
Experiences with Microservices at TuentiAndrés Viedma Peláez
 
Docker, Atomic Host and Kubernetes.
Docker, Atomic Host and Kubernetes.Docker, Atomic Host and Kubernetes.
Docker, Atomic Host and Kubernetes.Jooho Lee
 
Snabb Switch: Riding the HPC wave to simpler, better network appliances (FOSD...
Snabb Switch: Riding the HPC wave to simpler, better network appliances (FOSD...Snabb Switch: Riding the HPC wave to simpler, better network appliances (FOSD...
Snabb Switch: Riding the HPC wave to simpler, better network appliances (FOSD...Igalia
 
Ruby in office time reboot
Ruby in office time rebootRuby in office time reboot
Ruby in office time rebootKentaro Goto
 
Python Streaming Pipelines on Flink - Beam Meetup at Lyft 2019
Python Streaming Pipelines on Flink - Beam Meetup at Lyft 2019Python Streaming Pipelines on Flink - Beam Meetup at Lyft 2019
Python Streaming Pipelines on Flink - Beam Meetup at Lyft 2019Thomas Weise
 
High performance network programming on the jvm oscon 2012
High performance network programming on the jvm   oscon 2012 High performance network programming on the jvm   oscon 2012
High performance network programming on the jvm oscon 2012 Erik Onnen
 
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...AboutYouGmbH
 
Rust kafka-5-2019-unskip
Rust kafka-5-2019-unskipRust kafka-5-2019-unskip
Rust kafka-5-2019-unskipGerard Klijs
 
Rubykaigi 2017-nishimotz-v6
Rubykaigi 2017-nishimotz-v6Rubykaigi 2017-nishimotz-v6
Rubykaigi 2017-nishimotz-v6Takuya Nishimoto
 
Dennis Benkert & Matthias Lübken - Patterns in a containerized world? - code....
Dennis Benkert & Matthias Lübken - Patterns in a containerized world? - code....Dennis Benkert & Matthias Lübken - Patterns in a containerized world? - code....
Dennis Benkert & Matthias Lübken - Patterns in a containerized world? - code....AboutYouGmbH
 
Ruby3x3: How are we going to measure 3x
Ruby3x3: How are we going to measure 3xRuby3x3: How are we going to measure 3x
Ruby3x3: How are we going to measure 3xMatthew Gaudet
 
Highly concurrent yet natural programming
Highly concurrent yet natural programmingHighly concurrent yet natural programming
Highly concurrent yet natural programmingInfinit
 
Packaging perl (LPW2010)
Packaging perl (LPW2010)Packaging perl (LPW2010)
Packaging perl (LPW2010)p3castro
 

Was ist angesagt? (20)

From V8 to Modern Compilers
From V8 to Modern CompilersFrom V8 to Modern Compilers
From V8 to Modern Compilers
 
20140626 red dotrubyconf2014
20140626 red dotrubyconf201420140626 red dotrubyconf2014
20140626 red dotrubyconf2014
 
How to develop the Standard Libraries of Ruby?
How to develop the Standard Libraries of Ruby?How to develop the Standard Libraries of Ruby?
How to develop the Standard Libraries of Ruby?
 
SIL - All you need to know about Swift Intermediate Language
SIL - All you need to know about Swift Intermediate LanguageSIL - All you need to know about Swift Intermediate Language
SIL - All you need to know about Swift Intermediate Language
 
Experiences with Microservices at Tuenti
Experiences with Microservices at TuentiExperiences with Microservices at Tuenti
Experiences with Microservices at Tuenti
 
Docker, Atomic Host and Kubernetes.
Docker, Atomic Host and Kubernetes.Docker, Atomic Host and Kubernetes.
Docker, Atomic Host and Kubernetes.
 
Hello istio
Hello istioHello istio
Hello istio
 
Snabb Switch: Riding the HPC wave to simpler, better network appliances (FOSD...
Snabb Switch: Riding the HPC wave to simpler, better network appliances (FOSD...Snabb Switch: Riding the HPC wave to simpler, better network appliances (FOSD...
Snabb Switch: Riding the HPC wave to simpler, better network appliances (FOSD...
 
eZ Publish nextgen
eZ Publish nextgeneZ Publish nextgen
eZ Publish nextgen
 
Ruby in office time reboot
Ruby in office time rebootRuby in office time reboot
Ruby in office time reboot
 
Python Streaming Pipelines on Flink - Beam Meetup at Lyft 2019
Python Streaming Pipelines on Flink - Beam Meetup at Lyft 2019Python Streaming Pipelines on Flink - Beam Meetup at Lyft 2019
Python Streaming Pipelines on Flink - Beam Meetup at Lyft 2019
 
High performance network programming on the jvm oscon 2012
High performance network programming on the jvm   oscon 2012 High performance network programming on the jvm   oscon 2012
High performance network programming on the jvm oscon 2012
 
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...
 
Rust kafka-5-2019-unskip
Rust kafka-5-2019-unskipRust kafka-5-2019-unskip
Rust kafka-5-2019-unskip
 
Rubykaigi 2017-nishimotz-v6
Rubykaigi 2017-nishimotz-v6Rubykaigi 2017-nishimotz-v6
Rubykaigi 2017-nishimotz-v6
 
Flutter 2
Flutter 2Flutter 2
Flutter 2
 
Dennis Benkert & Matthias Lübken - Patterns in a containerized world? - code....
Dennis Benkert & Matthias Lübken - Patterns in a containerized world? - code....Dennis Benkert & Matthias Lübken - Patterns in a containerized world? - code....
Dennis Benkert & Matthias Lübken - Patterns in a containerized world? - code....
 
Ruby3x3: How are we going to measure 3x
Ruby3x3: How are we going to measure 3xRuby3x3: How are we going to measure 3x
Ruby3x3: How are we going to measure 3x
 
Highly concurrent yet natural programming
Highly concurrent yet natural programmingHighly concurrent yet natural programming
Highly concurrent yet natural programming
 
Packaging perl (LPW2010)
Packaging perl (LPW2010)Packaging perl (LPW2010)
Packaging perl (LPW2010)
 

Ähnlich wie Should i Go there

Ähnlich wie Should i Go there (20)

kotlin-nutshell.pptx
kotlin-nutshell.pptxkotlin-nutshell.pptx
kotlin-nutshell.pptx
 
Developer’s viewpoint on swift programming language
Developer’s viewpoint on swift programming languageDeveloper’s viewpoint on swift programming language
Developer’s viewpoint on swift programming language
 
Ruby basics
Ruby basicsRuby basics
Ruby basics
 
Introduction to Kotlin for Android developers
Introduction to Kotlin for Android developersIntroduction to Kotlin for Android developers
Introduction to Kotlin for Android developers
 
Dart programming language
Dart programming languageDart programming language
Dart programming language
 
Go Programming language, golang
Go Programming language, golangGo Programming language, golang
Go Programming language, golang
 
c++ Unit III - PPT.pptx
c++ Unit III - PPT.pptxc++ Unit III - PPT.pptx
c++ Unit III - PPT.pptx
 
Python Tutorial Part 1
Python Tutorial Part 1Python Tutorial Part 1
Python Tutorial Part 1
 
Denis Lebedev, Swift
Denis  Lebedev, SwiftDenis  Lebedev, Swift
Denis Lebedev, Swift
 
Functions, List and String methods
Functions, List and String methodsFunctions, List and String methods
Functions, List and String methods
 
Clojure intro
Clojure introClojure intro
Clojure intro
 
The Swift Compiler and Standard Library
The Swift Compiler and Standard LibraryThe Swift Compiler and Standard Library
The Swift Compiler and Standard Library
 
Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++
 
Rust All Hands Winter 2011
Rust All Hands Winter 2011Rust All Hands Winter 2011
Rust All Hands Winter 2011
 
Matlab lec1
Matlab lec1Matlab lec1
Matlab lec1
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 

Kürzlich hochgeladen

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024The Digital Insurer
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfOverkill Security
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusZilliz
 

Kürzlich hochgeladen (20)

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 

Should i Go there

  • 2. Go • Go (or GoLang) is a general purpose programming language created by Google. • First version appeared at 2009. • Some well known open-source projects using Go: • Docker. • Kubernetes. • OpenShift.
  • 3. Go • Go language characteristics: • Statically-typed. • Compiled. • Garbage-collected. • Memory-safe.
  • 4. Go • There is no large runtime to install like in Java. • Great concurrency model (goroutines & channels). • Extremely efficient. • Large standard library. • AOT compilation. • Fast compilation. • Small and clear language spec. • Integrates well with C.
  • 5. Go vs. Java • Java is definitely more high level. • Provides generics and classes. • However, comes with a large runtime (less in Java 9). • Containers become large. • Not suited for command line utilities. • First interpreted then compiled. • Large external dependencies.
  • 6. Go vs. Python • Performance, Performance, Performance. • Python is dynamically typed. • However, Python has its use-cases (e.g., machine learning).
  • 7. Go vs. C • Go is safer. • Provides superior concurrency utilities. • More productive.
  • 8. strings • Strings in Go are built-in types. • They are not pointers (like in Java) but values. • They are never null (or nil in Go). • Note that strings are immutable in Go.
  • 9. Arrays • Arrays have fixed size in Go. • Declared with [n]T. • Where n is the size and T is the element type. • The built-in len function returns the size of the array. • Accessing an element is done with arr[idx]. • Index is zero-based.
  • 10. Arrays • The length of the array is part of its type. • For example: var arr1 [10]int var arr2 [10]int var arr3 [4]int arr1 = arr2 arr2 = arr1 // Compilation error // not of the same type arr3 = arr2
  • 11. Arrays • Important: arrays are values. They are copied on assignment or when sent as a function argument. • In most cases this will result in an incorrect result or in a performance hit.
  • 12. Slices • Usually, you’ll not work with arrays directly (due to the fact that they are values). • You’ll work with slices. • A slice is a reference to an array. • It is analogous to an array pointer.
  • 13. Slices • A slice holds a pointer inside the array, a length and the capacity (the maximum allowed length inside the underlying array). • Example: // array var arr4 [10]int // slice var slice1 []int = arr4[0:2] // prints 2 println(len(slice1)) // prints 10 println(cap(slice1)) // slice slice2 := arr4[2:4] // prints 2 println(len(slice2)) // prints 8 println(cap(slice2))
  • 14. Slices Representation • A slice, behind the scenes, contains a pointer into the array, a length and capacity. • The capacity is the maximum allowed growth of the slice (from current location until the end of the underlying array). • s2 = s2[:cap(s2)+1] • Runtime error!
  • 15. Working with Slices • Usually, you create slices with the make function. • The make function accepts a type, length and capacity. • Go provides with 2 additional functions to work with slices: • copy – accepts destination and source and returns the number of elements copied (min(len(dst), len(src))). • append – appends elements to a slice, possibly allocates a new underlying array.
  • 16. Important • Note that the underlying array will not be collected (disposed) as long as there is a slice pointing into it. • May cause memory problems in some cases. • Consider using copy/append to avoid these situations!
  • 17. Ranges • Using the range keyword you can iterate over: slices, array, string or map. • Produces two iteration variables: • Arrays, slices, pointers to arrays – idx, value. • maps – key, value. • strings – byte index, rune. • Can also work on channels (discussed later).
  • 18. Ranges • Important, the range expression is evaluated once. • Constant-sized arrays will not cause any range evaluation. • nil values will produce zero iterations!
  • 19. if/else • If statements resemble for loops (without the step clause). • The initializer part is optional. There are no parenthesis. • Example: x := 5 if y := x * x; y < 20 { println("if") } else { println("else") } if x < 10 { println("again") }
  • 20. switch • Go provides a switch statement. • No fall-through unless explicitly stated. • Example: x := 5 switch z := x*x; z { case 25: println("hello") println("world") fallthrough case 30: println("false") }
  • 21. switch • If you drop the condition, you get a concise syntax for if-else chains. • Due to the fact that case clauses can contain expressions. • Example: x := rand.Int() switch { case x < 10: println("x < 10") case x > 10: println("x > 10") default: println("x == 10") }
  • 22. Structs • A struct is a group of fields. • General syntax: type name struct { fields }. • Example: type Point struct { x int y int }
  • 23. Structs • Struct instances are created like the following: var p1 Point = Point{10,20} p2 := Point{10,20} p3 := Point{y:20, x: 10}
  • 24. Structs • You can access a struct field by using dot (.) followed by the field name. • Example: var p1 Point = Point{10,20} // print the x+y value: println(p1.x + p1.y)
  • 25. Functions • Function declaration in Go starts with the func keyword. • General syntax is: func name (args) return_type { body}. • Example: func greaterThan10(x int) bool { return x > 10 }
  • 26. Functions • Functions in Go can return multiple results. • You can even name the results. • If you name the results, it defines the variables at the beginning of the function. • A return without values, will automatically return those variables. • Let’s see an example…
  • 27. Multiple Results func DivAndMod(a,b int) (q,r int) { q = a / b r = a % b return } func main() { q,r := DivAndMod(10,3) // prints 3 println(q) // prints 1 println(r)
  • 28. Naming Conventions • As you may have noticed, Go uses CamelCase notation. • Functions that are exported (i.e., part of your public API) should start with an uppercase letter. • If it’s part of your internal API, it should start with a lowercase letter. • The same goes for variables.
  • 29. Exports • So, what is exported from your package (i.e., visible to other packages)? • Everything that starts with an uppercase. • “export” is not a keyword.
  • 30. Pointers • Go has pointers. • A pointer holds the memory address of a variable. • Note that Go doesn’t provide pointer arithmetic like C. • The zero value for a pointer is: nil.
  • 31. Pointers • Note that Go is a by-value language. • It means that when an argument is passed to a function, it will pass by value. • I.e., its value will be copied. • Pointers are just a memory address number.
  • 32. Pointers • Go guarantees that the object pointed by the pointer will be ”alive” and reachable as long as it is referenced. • Due to the fact that there is no pointer arithmetic, pointers and arrays are not equal like in C.
  • 33. Pointers Syntax • Pointer types are regular types preceded by an asterisk (*). • Example: func f1(a int) { a = 8 } func f2(a *int) { *a = 8 } func main() { xx := 10 f1(xx) println(xx) // prints 10 // passing the pointer to xx f2(&xx) println(xx) // prints 8
  • 34. Defer • The defer keyword allows to provide some code that will be executed when the function returns. • When several defer statements are provided, they are executed in a LIFO manner. • One classic usecase is to provide resource-cleaning operations. • Let’s see an example…
  • 35. Defer func boo() { defer println("This line is deffered 1!") defer println("This line is deffered 2!") println("This line is NOT deferred!") }
  • 36. The Blank Identifier • The blank identifier, ‘_’, is used as a write-only, immediately discarded value. • Let’s discuss several use-cases for it: • Discard some values from multiple results of a function. • Silence work-in-progress, unused imports. • Imports for side-effects. • Interface checks.
  • 37. Maps • One of the basic data-structures in Go is: map. • General syntax for the type: map[keyType]valueType. • For example, a map from int to string is the type: map[int]string. • Note that the key type must be a comparable.
  • 38. Maps • If the key doesn’t exist, we get the zero value (pun intended). • The built-in function len returns the number of elements in the map. • The delete function removes a key-value pair from the map. • You can use two-value assignment to test if the key exists: m := make(map[int]string) m[1] = "hello" s1 := m[2] s2, ok := m[2] // prints false println(ok)
  • 39. Comparable Types • Comparable types in Go are: • string, numbers, booleans, pointers, channels, interfaces. • Structs and arrays that are composed only of these types. • Non-comparable: functions, slices, maps. • Comparable types can be compared using ==.
  • 40. Types • You can introduce new types with the type keyword. • Note that two types are distinct even if they share the same layout. • Example: type XY int type ZZ int func main() { var uu XY = 40 var ii ZZ = 80 // doesn't compile ii = uu
  • 41. No Classes • Go is not your classic Object Oriented programming language. • There are no classes in Go. • Instead it has interfaces!
  • 42. Interfaces • An interface is a collection of methods. • Example: type Vehicle interface { NumberOfWheels() int HasMotor() bool }
  • 43. Interfaces • So, an interface provides no implementation. • A type can implement the interface implicitly. • For example: type Bicycle struct { model string } func (b Bicycle) NumberOfWheels() int { return 2 } func (b Bicycle) HasMotor() bool { return false }
  • 44. Interfaces • There is no classic Object-Oriented virtual table or inheritance in go. • Instead, if there is an implementation for the interface methods for the type, the implementation will be invoked: func ShowVehicle(v Vehicle) { println(v.NumberOfWheels()) } func main() { bi := Bicycle{"BMX"} ShowVehicle(bi)
  • 45. Dynamic Types • In Go, each variable has a static type and a dynamic type. • The static type is the type stated at the declaration site. • For non-interface types, it is also always the dynamic type. • For interface types, the dynamic type is the actual type stored in the variable at runtime.
  • 46. Dynamic Types • Example: var xx int = 9 var yy interface{} = 9 // doesn't compile xx = "hello" // compiles yy = "hello"
  • 47. Stringer • One of the popular interfaces in Go is Stringer. • Defined in the fmt package. • Comes with a single method: • String() string. • Types implementing this interface can be easily converted into strings. • Think, should there be a default implementation for this interface (like in Java)?
  • 48. Concurrency • Concurrency in Go revolves around CSP (Communicating Sequential Processes). • Instead of threads, Go has goroutines. • Very lightweight (compared to thread). • Resemble actors (thought not the same).
  • 49. Goroutines • You have have thousands or more concurrent goroutines. • You start a goroutine with the go keyword. • When a coroutine “blocks” the same thread will be allocated with another coroutine.
  • 50. Channels • Channels are the concurrency-communication mechanism in Go. • First class citizens. • Built-in keyword: chan. • Created with the make function. • Example: out := make(chan int)
  • 51. Channels • As you can see, channels are typed. • You can declare directionality: foo(x int, c chan<- int) foo(y int, c <-chan int) • If you don’t provide the arrow, you can both send and receive. • Like at the creation site.
  • 52. Channels vs. Actors • Actors are like a goroutine with a single input channel. • In go, you can read from the same channel with multiple goroutines. • Actor frameworks (e.g., Akka), are usually more feature-rich.
  • 53. Code Generations • Go has a lot of support for code-generation. • Go generate is a nice utility for running our code generation tool according to annotations. • Used heavily in: • Marshalling/Serialization. • Implementing complex interfaces (with good default type-specific implementations. E.g., Kubernetes code generation.