Docker is the preferred approach to running the examples from this repository.
- Build the image: build the Docker image locally
- Use the image: use the Docker image to run the examples in this repository
Build the Docker image by executing the following commands:
-
Clone this repository locally:
git clone https://github.com/akutz/go-generics-the-hard-way
-
Change directories to the root of the cloned repository:
cd go-generics-the-hard-way
-
Build the image:
docker build -t go-generics-the-hard-way .
And voilá, you have the image!
⭐ Please note the image is also available remotely and can obtained by executing the following commands:
docker pull akutz/go-generics-the-hard-way && \
docker tag akutz/go-generics-the-hard-way go-generics-the-hard-way
The image may be used one of three ways:
- Self-contained: run the examples from the image's filesystem
- Mount repository: run the examples from your local filesystem using the image
- VSCode Dev Container: run the examples in VS Code using the
Remote - Containers
extension
This method does not require cloning this repository locally since the Docker image includes the contents of this repository. For example, the following command will run the boxing benchmark using the examples sources from inside the image:
docker run -it --rm go-generics-the-hard-way \
go test -tags benchmarks -run Boxing -bench Boxing -benchmem -count 5 -v ./06-benchmarks/
Alternatively, it is also possible to run the examples cloned on your local filesystem using the Docker image. This has two benefits:
- The examples will be up-to-date
- It is easier to capture file output from the examples such as profiles
For example, the following command will:
- Run the boxing benchmark using the examples from the cloned repository
- Produce a memory profile
- Produce an SVG image from the memory profile
docker run -it --rm -v "$(pwd):/go-generics-the-hard-way" \
go-generics-the-hard-way bash -c ' \
go test -tags benchmarks -run Boxing -bench Boxing -benchmem -count 5 -memprofile memprofile.out -v ./06-benchmarks/ && \
go tool pprof -svg memprofile.out'
At the end there will be a file in the current, local directory named profile001.svg
that visualizes the memory profile produced from the benchmark.
Alternatively, it is also possible to run the examples through opening the cloned reposity in a VS Code devcontainer:
Note: This assumes you are using VS Code as your IDE
- To get started, install the extension
Remote - Containers
by Microsoft (ms-vscode-remote.remote-containers). - Open up your Command Palette and type or select
Remote-Containers: Reopen in Container
command.
- The extension should be able to pick up the settings described in the .devcontainer/devcontainer.json and build the Dockerfile
- Open up your terminal and you should be inside a container and at the project root.
- A good test is to do
go version
If you see go version go1.18beta2 linux/arm64
you should be good to go.
Next: Hello world