Workshop: Introduction to
the Programming Language
Go
Marcel Schneider
Goals of this Workshop
- Learn the Basics of Go
- Write, compile and run Go Apps
- Create a REST Webservice with Go
- Understand Error Handling, Testing, Object Orientation and Concurrency in Go
Go History
| 2007 |
Work on Go started at Google |
| 2012 |
Release Version 1.0 |
| 2012 |
first Go Conference in Denver/Colorado |
| 2022 |
Introduction of Generics in Release 1.18 |
| 2025 |
current Release is 1.25.1 |
Companies using Go
Motivation behind Go
- Dissatisfaction with existing Programming Languages like C++/Java
- address Pain Points in large scale Software Development
- "Language Design in the Service of Software Engineering" (Rob Pike)
Go Features
- Compilation into Machine Code
- static Typing (with Type Inference)
- Concurrency with lightweight Threads
- automatic Garbage Collection
- built-in Dependency Management
- completely OpenSource (BSD License)
- extensive standard Library (pkg.go.dev)
Language Characteristics
- Data Types: Basic Types, Arrays, Maps and Structs
- Pointers, but no Pointer Arithmetics
- C-inspired Syntax
- Packages used for Structuring Source Files
- Generics (since Release 1.18 on 15.03.2022)
- General: Focus on Minimalism and Simplicity
Exercise 1a: Soundex
- Phonetic Algorithm for Indexing Names by Sound, as pronounced in English
- Description of (engl.) Soundex Algorithm, see https://de.wikipedia.org/wiki/Soundex
- Examples: Golang → G452, Soundex → S532
Exercise 1a: Soundex Algorithm
- first Letter uppercase, followed by exactly 3 Numbers (Soundex → S532)
- Mapping of Letters see Table
- no Mapping for A/E/I/O/U/H/W/Y
- no consecutive equal Numbers
- ensure length 4, add Zeros or cut
- Hallo → H400, Entwicklertag → E532
Exercise 1a: Soundex Coding
Error Handling
- no try/catch, no Exceptions
- Error as additional Return Type
- Error == nil means no Error
type error interface {
Error() string
}
Exercise 1b: Soundex Errors
Exercise 1c: Soundex Testify
Next Topic: Object Orientation
- supported, but without Classes
- Methods can be defined on Structs
- Composition over Inheritance
- implicit Interface Implementation
Exercise 2: Movie Database
- REST Webservice which manages Movie Data
- HTTP Request Mapping with Go Standard Library
- Usage of hardcoded Example Movies
Exercise 2: Implement REST Call
- navigate to Subfolder moviedb-go
- implement Endpoint GET /movies/{id}
- create InMemoryRepository (File inmemory.go) which fulfills the existing Interface MovieRepository
- see example-data.go for movie test data
- test the Implementation with your Browser
Next Topic: Concurrency
- Goroutine: lightweight Thread managed by the Go Runtime
- sync Package with Mutex/Locks and WaitGroups
- Concept of Channels for Goroutine Communication
- "Don't communicate by sharing Memory, share Memory by Communicating" (Rob Pike)
Exercise 3: Website Checker
- App which checks the Reachability of Websites
- currently the App executes the Checks sequentially
- Goal: Execute the Checks concurrently and use Channel Synchronization
Exercise 3: Website Checker
- navigate to Subfolder website-checker-go
- run the Checks concurrently
- transmit the Results to a Channel
- receive Results in main() and print them
- compare Running Time sequential vs. concurrent
Conclusion and Feedback
- Small Memory Footprint and fast Startup Time
- Lightweight Ecosystem
- Good online Documentation
- What did You like in Go (or not)?