Go Logo bIT Logo

Workshop: Introduction to
the Programming Language
Go

Marcel Schneider

Agenda for today

  1. Intro
  2. Go Basics & Soundex
  3. Coffee Break
  4. Object Orientation & Movie Database
  5. Concurrency & Website Checker
  6. Cool Down & Feedback

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

Tools you need locally

Helpful Links

Slides: bridgingit-gmbh.github.io/go-workshop
Git Repo: github.com/bridgingit-gmbh/go-workshop
Tour of Go
Standard Library
Go Playground

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)

DEMO: Hello World

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 Setup

  1. git clone -b workshop
    github.com/bridgingit-gmbh/go-workshop
  2. cd go-workshop/soundex-go
  3. go build
  4. ./soundex-go (.exe on Windows)

user@pc % ./soundex-go
implement me
            

Exercise 1a: Soundex Coding

  • Implement the Function in soundex.go
    
    package soundex
    
    func Soundex(input string) string {
        return "implement me"
    }
                        
  • Test your implementation
    
    user@pc % go test
    ok github.com/go-workshop/soundex-go/soundex 0.303s
                        

Error Handling

  • no try/catch, no Exceptions
  • Error as additional Return Type
  • Error == nil means no Error

type error interface {
    Error() string
}
            

DEMO: Error Handling

Exercise 1b: Soundex Errors

  • adjust Signature of Soundex Function
    
    package soundex
    
    func Soundex(input string) (string, error) {
        //your code here
        return result, err
    }
                      
  • return Error on empty Input String
  • check return values in main() and print the error
  • add a test for the error case

Exercise 1c: Soundex Testify

Next Topic: Object Orientation

  • supported, but without Classes
  • Methods can be defined on Structs
  • Composition over Inheritance
  • implicit Interface Implementation

DEMO: Object Orientation

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

  1. navigate to Subfolder moviedb-go
  2. implement Endpoint GET /movies/{id}
  3. create InMemoryRepository (File inmemory.go) which fulfills the existing Interface MovieRepository
  4. see example-data.go for movie test data
  5. 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)

Demo: Channel

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

  1. navigate to Subfolder website-checker-go
  2. run the Checks concurrently
  3. transmit the Results to a Channel
  4. receive Results in main() and print them
  5. 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)?
Gopher