From 9f51b5e7d11c8f6e1d16dbe5b5a1d17f6cd2a300 Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Sat, 29 Oct 2022 23:22:52 +0100 Subject: [PATCH] feat: add move --- experiments/go/move/.dockerignore | 4 ++ experiments/go/move/Dockerfile | 22 ++++++++++ experiments/go/move/main.go | 60 ++++++++++++++++++++++++++ experiments/go/move/movement_string.go | 27 ++++++++++++ 4 files changed, 113 insertions(+) create mode 100644 experiments/go/move/.dockerignore create mode 100644 experiments/go/move/Dockerfile create mode 100644 experiments/go/move/main.go create mode 100644 experiments/go/move/movement_string.go diff --git a/experiments/go/move/.dockerignore b/experiments/go/move/.dockerignore new file mode 100644 index 0000000..69804bc --- /dev/null +++ b/experiments/go/move/.dockerignore @@ -0,0 +1,4 @@ +* +!*.go +!go.sum +!go.mod diff --git a/experiments/go/move/Dockerfile b/experiments/go/move/Dockerfile new file mode 100644 index 0000000..46b71c6 --- /dev/null +++ b/experiments/go/move/Dockerfile @@ -0,0 +1,22 @@ +# Build the application +FROM docker.io/library/golang:1.19.2-alpine3.16@sha256:46752c2ee3bd8388608e41362964c84f7a6dffe99d86faeddc82d917740c5968 AS builder + +ENV GOOS=linux +ENV GOARCH=amd64 +ENV CGO_ENABLED=0 + +WORKDIR /build + +COPY . . + +RUN \ + go mod tidy \ + && go build -ldflags="-s -w" -a -o /build/move . + +FROM scratch + +COPY --from=builder /build/move /move + +USER 3000:3000 + +ENTRYPOINT ["/move"] diff --git a/experiments/go/move/main.go b/experiments/go/move/main.go new file mode 100644 index 0000000..04c4aef --- /dev/null +++ b/experiments/go/move/main.go @@ -0,0 +1,60 @@ +package main + +import ( + "fmt" + "strings" + "errors" + "flag" + "os" +) + +//go:generate stringer -type=Movement +type Movement int + +const ( + Unknown Movement = iota + Forward + Backward + Left + Right +) + +func newMovement(movement string) (Movement) { + switch strings.ToLower(movement) { + case "forward": + return Forward + case "backward": + return Backward + case "left": + return Left + case "right": + return Right + default: + return Unknown + } +} + +func main() { + direction := flag.String("direction", "", "The direction to move.") + flag.Parse() + + if err := run(*direction); err != nil { + fmt.Printf("Error: %v.\n", err) + os.Exit(1) + } +} + +func run(direction string) error { + if len(direction) == 0 { + return errors.New("direction is not set") + } + + move := newMovement(direction) + if move == Unknown { + return errors.New("unknown direction") + } + + fmt.Printf("I moved %s.\n", move) + + return nil +} diff --git a/experiments/go/move/movement_string.go b/experiments/go/move/movement_string.go new file mode 100644 index 0000000..c8ec3b1 --- /dev/null +++ b/experiments/go/move/movement_string.go @@ -0,0 +1,27 @@ +// Code generated by "stringer -type=Movement"; DO NOT EDIT. + +package main + +import "strconv" + +func _() { + // An "invalid array index" compiler error signifies that the constant values have changed. + // Re-run the stringer command to generate them again. + var x [1]struct{} + _ = x[Unknown-0] + _ = x[Forward-1] + _ = x[Backward-2] + _ = x[Left-3] + _ = x[Right-4] +} + +const _Movement_name = "UnknownForwardBackwardLeftRight" + +var _Movement_index = [...]uint8{0, 7, 14, 22, 26, 31} + +func (i Movement) String() string { + if i < 0 || i >= Movement(len(_Movement_index)-1) { + return "Movement(" + strconv.FormatInt(int64(i), 10) + ")" + } + return _Movement_name[_Movement_index[i]:_Movement_index[i+1]] +}