Skip to main content

How to use contexts in Go

· 2 min read
  • example
    • request URL in a web app
    • connection status e.g. to stop processing when the client disconnects or times out
  • context package
  • context.TODO() creates an empty placeholder context
  • context.Background also creates an empty context
    • deciding which to use is about signaling intent to other developers
    • use Background when you "intend to start a known context"
    • it's a good default option if you aren't sure
  • each layer of an app can add its own information to the context using context.WithValue(ctx, k, v) to create a new context containing that new value
    • this new context will have the original context as the "parent context"
    • contexts are immutable
  • When to use a value in a context vs explicit param
    • depends on what the function is doing with the value
    • e.g. if you have a function that explicitly operates on a username, you should pass that in rather than relying on the context
    • mostly about signaling developer intent
  • Ending a context
    • using Done returns a channel that can be watched for values. it will never be written to until the context is done so you can check it to know when to stop processing
    • can only perform one channel operation per select but can put that select in a loop
    • adding default to the select will keep it from blocking
    • in the example of a loop that tries to either read from Done or from another channel, default just means it starts another loop immediately instead of waiting. This is called a "busy loop". The external behavior is the same though
    • context.WithCancel returns (new context, cancellation function)
      • canceling a context does not automatically cancel the parent context
    • context canceled is a common error in http apps when the client disconnects mid-response
  • context.WithDeadline
    • will cancel context automatically
    • error will be context deadline exceeded
    • there is a convenience wrapper around this called context.WithTimeout

References

Environment variables used by Golang

· 7 min read

see go help environment for list of environment variables used by go, including a few defined elsewhere.

GOROOT

The root the go directory structure

Output of go help env

captured 2023-12-20

$ go help env
usage: go env [-json] [-u] [-w] [var ...]

Env prints Go environment information.

By default env prints information as a shell script
(on Windows, a batch file). If one or more variable
names is given as arguments, env prints the value of
each named variable on its own line.

The -json flag prints the environment in JSON format
instead of as a shell script.

The -u flag requires one or more arguments and unsets
the default setting for the named environment variables,
if one has been set with 'go env -w'.

The -w flag requires one or more arguments of the
form NAME=VALUE and changes the default settings
of the named environment variables to the given values.

For more about environment variables, see 'go help environment'.

Output of go help environment

captured 2023-12-20

$ go help environment
The go command and the tools it invokes consult environment variables
for configuration. If an environment variable is unset, the go command
uses a sensible default setting. To see the effective setting of the
variable <NAME>, run 'go env <NAME>'. To change the default setting,
run 'go env -w <NAME>=<VALUE>'. Defaults changed using 'go env -w'
are recorded in a Go environment configuration file stored in the
per-user configuration directory, as reported by os.UserConfigDir.
The location of the configuration file can be changed by setting
the environment variable GOENV, and 'go env GOENV' prints the
effective location, but 'go env -w' cannot change the default location.
See 'go help env' for details.

General-purpose environment variables:

GO111MODULE
Controls whether the go command runs in module-aware mode or GOPATH mode.
May be "off", "on", or "auto".
See https://golang.org/ref/mod#mod-commands.
GCCGO
The gccgo command to run for 'go build -compiler=gccgo'.
GOARCH
The architecture, or processor, for which to compile code.
Examples are amd64, 386, arm, ppc64.
GOBIN
The directory where 'go install' will install a command.
GOCACHE
The directory where the go command will store cached
information for reuse in future builds.
GOMODCACHE
The directory where the go command will store downloaded modules.
GODEBUG
Enable various debugging facilities. See 'go doc runtime'
for details.
GOENV
The location of the Go environment configuration file.
Cannot be set using 'go env -w'.
Setting GOENV=off in the environment disables the use of the
default configuration file.
GOFLAGS
A space-separated list of -flag=value settings to apply
to go commands by default, when the given flag is known by
the current command. Each entry must be a standalone flag.
Because the entries are space-separated, flag values must
not contain spaces. Flags listed on the command line
are applied after this list and therefore override it.
GOINSECURE
Comma-separated list of glob patterns (in the syntax of Go's path.Match)
of module path prefixes that should always be fetched in an insecure
manner. Only applies to dependencies that are being fetched directly.
GOINSECURE does not disable checksum database validation. GOPRIVATE or
GONOSUMDB may be used to achieve that.
GOOS
The operating system for which to compile code.
Examples are linux, darwin, windows, netbsd.
GOPATH
For more details see: 'go help gopath'.
GOPROXY
URL of Go module proxy. See https://golang.org/ref/mod#environment-variables
and https://golang.org/ref/mod#module-proxy for details.
GOPRIVATE, GONOPROXY, GONOSUMDB
Comma-separated list of glob patterns (in the syntax of Go's path.Match)
of module path prefixes that should always be fetched directly
or that should not be compared against the checksum database.
See https://golang.org/ref/mod#private-modules.
GOROOT
The root of the go tree.
GOSUMDB
The name of checksum database to use and optionally its public key and
URL. See https://golang.org/ref/mod#authenticating.
GOTMPDIR
The directory where the go command will write
temporary source files, packages, and binaries.
GOVCS
Lists version control commands that may be used with matching servers.
See 'go help vcs'.
GOWORK
In module aware mode, use the given go.work file as a workspace file.
By default or when GOWORK is "auto", the go command searches for a
file named go.work in the current directory and then containing directories
until one is found. If a valid go.work file is found, the modules
specified will collectively be used as the main modules. If GOWORK
is "off", or a go.work file is not found in "auto" mode, workspace
mode is disabled.

Environment variables for use with cgo:

AR
The command to use to manipulate library archives when
building with the gccgo compiler.
The default is 'ar'.
CC
The command to use to compile C code.
CGO_ENABLED
Whether the cgo command is supported. Either 0 or 1.
CGO_CFLAGS
Flags that cgo will pass to the compiler when compiling
C code.
CGO_CFLAGS_ALLOW
A regular expression specifying additional flags to allow
to appear in #cgo CFLAGS source code directives.
Does not apply to the CGO_CFLAGS environment variable.
CGO_CFLAGS_DISALLOW
A regular expression specifying flags that must be disallowed
from appearing in #cgo CFLAGS source code directives.
Does not apply to the CGO_CFLAGS environment variable.
CGO_CPPFLAGS, CGO_CPPFLAGS_ALLOW, CGO_CPPFLAGS_DISALLOW
Like CGO_CFLAGS, CGO_CFLAGS_ALLOW, and CGO_CFLAGS_DISALLOW,
but for the C preprocessor.
CGO_CXXFLAGS, CGO_CXXFLAGS_ALLOW, CGO_CXXFLAGS_DISALLOW
Like CGO_CFLAGS, CGO_CFLAGS_ALLOW, and CGO_CFLAGS_DISALLOW,
but for the C++ compiler.
CGO_FFLAGS, CGO_FFLAGS_ALLOW, CGO_FFLAGS_DISALLOW
Like CGO_CFLAGS, CGO_CFLAGS_ALLOW, and CGO_CFLAGS_DISALLOW,
but for the Fortran compiler.
CGO_LDFLAGS, CGO_LDFLAGS_ALLOW, CGO_LDFLAGS_DISALLOW
Like CGO_CFLAGS, CGO_CFLAGS_ALLOW, and CGO_CFLAGS_DISALLOW,
but for the linker.
CXX
The command to use to compile C++ code.
FC
The command to use to compile Fortran code.
PKG_CONFIG
Path to pkg-config tool.

Architecture-specific environment variables:

GOARM
For GOARCH=arm, the ARM architecture for which to compile.
Valid values are 5, 6, 7.
GO386
For GOARCH=386, how to implement floating point instructions.
Valid values are sse2 (default), softfloat.
GOAMD64
For GOARCH=amd64, the microarchitecture level for which to compile.
Valid values are v1 (default), v2, v3, v4.
See https://golang.org/wiki/MinimumRequirements#amd64
GOMIPS
For GOARCH=mips{,le}, whether to use floating point instructions.
Valid values are hardfloat (default), softfloat.
GOMIPS64
For GOARCH=mips64{,le}, whether to use floating point instructions.
Valid values are hardfloat (default), softfloat.
GOPPC64
For GOARCH=ppc64{,le}, the target ISA (Instruction Set Architecture).
Valid values are power8 (default), power9.
GOWASM
For GOARCH=wasm, comma-separated list of experimental WebAssembly features to use.
Valid values are satconv, signext.

Special-purpose environment variables:

GCCGOTOOLDIR
If set, where to find gccgo tools, such as cgo.
The default is based on how gccgo was configured.
GOEXPERIMENT
Comma-separated list of toolchain experiments to enable or disable.
The list of available experiments may change arbitrarily over time.
See src/internal/goexperiment/flags.go for currently valid values.
Warning: This variable is provided for the development and testing
of the Go toolchain itself. Use beyond that purpose is unsupported.
GOROOT_FINAL
The root of the installed Go tree, when it is
installed in a location other than where it is built.
File names in stack traces are rewritten from GOROOT to
GOROOT_FINAL.
GO_EXTLINK_ENABLED
Whether the linker should use external linking mode
when using -linkmode=auto with code that uses cgo.
Set to 0 to disable external linking mode, 1 to enable it.
GIT_ALLOW_PROTOCOL
Defined by Git. A colon-separated list of schemes that are allowed
to be used with git fetch/clone. If set, any scheme not explicitly
mentioned will be considered insecure by 'go get'.
Because the variable is defined by Git, the default value cannot
be set using 'go env -w'.

Additional information available from 'go env' but not read from the environment:

GOEXE
The executable file name suffix (".exe" on Windows, "" on other systems).
GOGCCFLAGS
A space-separated list of arguments supplied to the CC command.
GOHOSTARCH
The architecture (GOARCH) of the Go toolchain binaries.
GOHOSTOS
The operating system (GOOS) of the Go toolchain binaries.
GOMOD
The absolute path to the go.mod of the main module.
If module-aware mode is enabled, but there is no go.mod, GOMOD will be
os.DevNull ("/dev/null" on Unix-like systems, "NUL" on Windows).
If module-aware mode is disabled, GOMOD will be the empty string.
GOTOOLDIR
The directory where the go tools (compile, cover, doc, etc...) are installed.
GOVERSION
The version of the installed Go tree, as reported by runtime.Version.

JSON Web Tokens (JWT)

· One min read

Structure

  • alg = algorithm use
    • uses an agreed upon subset of crypto algorithms
  • kid = key identifier i.e. which key to use from the JWKS
  • typ = type, usually "JWT"

Standard Claims

  • iss = Issuer
  • aud = Audience, e.g. the consuming site
  • iat = "Issued at", in Unix Epoch Time
  • exp = "Expired at", in Unix Epoch Time
  • sub = Subject, e.g. the ID of the current user
  • additional app-defined claims

References

Combinator

· One min read
  • A combinator is any function which only depends on its inputs1
  • Often used as shorthand for "Combinator Pattern" or "Combinator library".
    • Combinator libraries are libraries of functions that take multiple things of the same type as input, and produce a thing of that same type as output2

Footnotes

  1. Wlashin, Scott. “Understanding Parser Combinators: A Deep Dive.” Presented at the NDC 2016, Oslo, Norway, August 8, 2016. https://youtu.be/RDalzi7mhdY?t=833

  2. Combinator (Haskell Wiki)

F#

· One min read

Syntax

Anonymous functions

As I like to say: fun is a keyword in F#

-- [[Scott Wlashin]], probably

let succ n = x + 1
let succ' = fun x -> x + 1

Records

type MyRecord =
{ X: int
Y: int
Z: int }

Active Patterns

  • basically, dynamic patterns

guards are great for one-off matches. But if there are certain guards that you use over and over, consider using active patterns instead.

-- Scott Wlashin cite:[Wlashin2012a]

References

Functor

· One min read

Represents a mapping between categories

Functions

class Functor f where
fmap :: (a -> b) -> f a -> f b
(<$) :: a -> f b -> f a

Operators

  • <$> - infix form of fmap

Laws

fmap id = id
fmap (f . g) = fmap f . fmap g

Git

· One min read

Environment variables

  • EDITOR

Commands

mergetool

Merge a directory by passing it as final argument

difftool

Diff two branches

Option --dir-diff

Diff directories instead of files

Integers

· One min read

Unsigned integers

The minimum value for all unsigned integers is by definition 0, so only max value is listed here

bitsmaxmax (exact)
8$2^8 - 1$255
16$2^16 - 1$
32$2^32 - 1$4,294,967,295
64$2^64 - 1$
128$2^128 - 1$

Signed integers

bitsminmin (exact)maxmax (exact)
8$-2^7$$2^7 - 1$
16$-2^15$$2^15 - 1$
32$-2^31$$2^31 - 1$2,147,483,647
64$-2^63$$2^63 - 1$
128$-2^127$$2^127 - 1$

Kubernetes

· 2 min read

Kinds

DaemonSet

  • used to ensure that the same pod is running on all nodes in the cluster and that that pod is started before other pods
  • Great for logging or management agents that need to run on every node

Deployment

  • abbr: deploy
  • used for reliability, load balancing and scaling
  • creates a ReplicaSet
  • uses declarative model
    • allows you to perform rolling updates with zero downtime

ReplicaSet

  • abbr: rs
  • used to maintain a set of replica pods running at all times
  • will add or delete Pod resources as necessary

StatefulSet

  • Designed for stateful applications, and maintain a sticky identity for each of the pods
  • Ideal for applications that need stable network identifiers, persistent storage, ordered deployment and ordered automated rolling updates.

Pod

  • Abstraction over one or more containers
  • One container for primary workload of the pod
    • zero or more containers, called "sidecars"
      • may provide additional functionality
      • usually more cross-cutting concerns, e.g. observability

Service

abbr:: svc

  • configures internal [[DNS]]
  • uses selector field to choose pods based on labels

Misc

Get pod logs

$ kubectl logs <pod-name>