Skip to content

boriwo/golang

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A Brief Overview of Go

Goals

  • introduction to the go programming language (golang) for developers familiar with other languages
  • from practitioners for practitioners
  • a little bit of the history of go, comparison with other languages (chart)
  • many tiny fully self-contained coding examples to exemplify common coding patterns and pitfalls in golang
  • distilled version: save you time reading a thick textbook or watch hours of tutorials (maybe)
  • focus on relevant features and examples

Resources

Golang Core

Talks

The Language - Syntax and Concepts

  • simplicity - simple, concise and C-inspired syntax
  • shadowing - shadowing variables based on scope
  • interface - interface, type casts and type conversions, no generics
  • pointers - pointers work very much like they do in C
  • functional - functional programming
  • variadic - introduction to variadic functions
  • defer - use defer when you don't want to forget to clean up
  • panic - recover from panics
  • mapsandslices - basic operations with maps and slices
  • safemap - a thread safe map implementation
  • jsoninjsonout - parse and serialize json payloads
  • duck - duck typing, no need to explicitly declare which interfaces are implemented
  • composition - composition over inheritance
  • oop - limitations regarding overriding functions, workaround using functional programming
  • concurrency - use go routines for concurrent execution
  • channel - creative use of goroutines and channels
  • workerpool - combine go routines with go channels to send jobs to workers in a pool
  • fanoutfanin - use worker pool to fan-out work and main thread to fan-in results
  • producerconsumer / producer sends work to consumer via channel
  • packages - importing and using packages, packages live inside a directory, everything uppercase is visible from outside the package
  • module - manage package versions and dependencies

Patterns

  • aws - use aws sdk to interact with amazon web services
  • options - functional options
  • context - use go context to manage lifecycle of long-running tasks with cancel and / or timeout
  • errors - errors.As(), errors.Is()
  • tinywebservice - a tiny web service using the http package from the standard library
  • controlc - capture ctrl-c OS signal, then wait for all goroutines to finish
  • shell execute shell command
  • shellong execute long running shell command
  • File I/O and HTTP
  • init function
  • Commandline parameters

Frameworks for Building RESTful Web Services

  • webservice - building REST APIs using viper, cobra, gorilla mux, zerolog and other frameworks

Useful Libraries

Special Features

  • embed - embed files in go binary with //go:embed
  • cgo - use cgo to call C library functions

The Tool Chain

Packages, Modules and go mod

go mod init
go mod tidy
go get somedomain.com/somepackage

Go Build

go build

Cross-Compiling

GOOS=linux GOARCH=arm go build -o myprogram myprogram.go

Go Test

https://www.digitalocean.com/community/tutorials/how-to-write-unit-tests-in-go-using-go-test-and-the-testing-package

Unit Tests

unittest

go test -v

https://pkg.go.dev/testing

Benchmarks

go test -bench=.

Note: Runtime of benchmarked function must converge!

https://dave.cheney.net/2013/06/30/how-to-write-benchmarks-in-go

Race Conditions

go test -race

Golden Files

goldie - easy testing with golden files

https://github.com/sebdah/goldie

Mock Services

Table Driven Testing

Linter

linter

go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
golangci-lint run

https://golangci-lint.run/usage/configuration/#config-file

Profiling

pprof - use pprof for heap profiling etc

Profile interactively for example with

go tool pprof http://localhost:8080/debug/pprof/heap

then use web command.

https://jvns.ca/blog/2017/09/24/profiling-go-with-pprof/

Go Releaser

Go Generate

Misc

  • Core features
    • compiled
    • statically typed
    • garbage collected
    • statically linked
    • C-style pointers
    • no objects (structs and composition instead)
    • no generics
    • no method overloading
    • conventions (tests, exported functions, init function etc.)
  • Strong support for concurrency built in (channels and concurrent execution as first class object, suited for multi-core architectures)
  • History of golang and resources, Rob Pike et al.
  • "Young" language
  • Easy to transition, especially if you know a little about C (structs, pointers)
  • Intellij Goland and debugging with https://github.com/go-delve/delve
  • Semantic versioning

About

A one-day workshop on the Go programming language

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published