Skip to content

Commit ece080e

Browse files
authored
Add go example (#171)
* Add go example * Review comments * Remove dockerfile
1 parent 863bc79 commit ece080e

File tree

12 files changed

+164
-14
lines changed

12 files changed

+164
-14
lines changed

.github/workflows/pre-release.yml

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,23 @@ on:
44
workflow_dispatch:
55
inputs:
66
restateVersion:
7-
description: 'Restate runtime version (without prepending v). Leave empty if you do not want to update it.'
7+
description: "Restate runtime version (without prepending v). Leave empty if you do not want to update it."
88
required: false
99
type: string
1010
sdkTypescriptVersion:
11-
description: 'sdk-typescript version (without prepending v). Leave empty if you do not want to update it.'
11+
description: "sdk-typescript version (without prepending v). Leave empty if you do not want to update it."
1212
required: false
1313
type: string
1414
sdkJavaVersion:
15-
description: 'sdk-java version (without prepending v). Leave empty if you do not want to update it.'
15+
description: "sdk-java version (without prepending v). Leave empty if you do not want to update it."
16+
required: false
17+
type: string
18+
sdkGoVersion:
19+
description: "sdk-go version (WITH the prepending v). Leave empty if you do not want to update it."
1620
required: false
1721
type: string
1822
cdkVersion:
19-
description: 'cdk version (without prepending v). Leave empty if you do not want to update it.'
23+
description: "cdk version (without prepending v). Leave empty if you do not want to update it."
2024
required: false
2125
type: string
2226

@@ -40,7 +44,7 @@ jobs:
4044
if: github.event.inputs.sdkTypescriptVersion != ''
4145
with:
4246
node-version: "19.x"
43-
registry-url: 'https://registry.npmjs.org'
47+
registry-url: "https://registry.npmjs.org"
4448
- name: Bump sdk-typescript
4549
if: github.event.inputs.sdkTypescriptVersion != ''
4650
run: ./.tools/update_node_examples.sh ${{ inputs.sdkTypescriptVersion }}
@@ -55,8 +59,8 @@ jobs:
5559
- uses: actions/setup-java@v3
5660
if: github.event.inputs.sdkJavaVersion != ''
5761
with:
58-
distribution: 'temurin'
59-
java-version: '17'
62+
distribution: "temurin"
63+
java-version: "17"
6064
- name: Setup Gradle
6165
if: github.event.inputs.sdkJavaVersion != ''
6266
uses: gradle/actions/setup-gradle@v3
@@ -67,13 +71,27 @@ jobs:
6771
if: github.event.inputs.sdkJavaVersion != ''
6872
run: ./.tools/run_jvm_tests.sh
6973

74+
# Bump Go SDK
75+
- uses: actions/setup-go@v5
76+
if: github.event.inputs.sdkGoVersion != ''
77+
with:
78+
go-version: "1.22"
79+
- name: Bump sdk-go
80+
if: github.event.inputs.sdkGoVersion != ''
81+
run: ./.tools/update_go_examples.sh ${{ inputs.sdkGoVersion }}
82+
- name: Run Go tests
83+
if: github.event.inputs.sdkGoVersion != ''
84+
run: ./.tools/run_go_tests.sh
85+
7086
- name: Create Pull Request
7187
uses: peter-evans/create-pull-request@v5
7288
with:
73-
title: "[GithubActions] Update Restate ${{ inputs.restateVersion != '' && format('Runtime {0} ', inputs.restateVersion) }}${{ inputs.sdkTypescriptVersion != '' && format('SDK-Typescript {0} ', inputs.sdkTypescriptVersion) }}${{ inputs.sdkJavaVersion != '' && format('SDK-Java {0} ', inputs.sdkJavaVersion) }}${{ inputs.cdkVersion != '' && format('CDK {0} ', inputs.cdkVersion) }}"
89+
title: "[GithubActions] Update Restate ${{ inputs.restateVersion != '' && format('Runtime {0} ', inputs.restateVersion) }}${{ inputs.sdkTypescriptVersion != '' && format('SDK-Typescript {0} ', inputs.sdkTypescriptVersion) }}${{ inputs.sdkJavaVersion != '' && format('SDK-Java {0} ', inputs.sdkJavaVersion) }}${{ inputs.cdkVersion != '' && format('CDK {0} ', inputs.cdkVersion) }}${{ inputs.sdkGoVersion != '' && format('SDK-Go {0} ', inputs.sdkGoVersion) }}"
7490
commit-message: "[GithubActions] Update Restate ${{ inputs.restateVersion != '' && format('Runtime {0} ', inputs.restateVersion) }}${{ inputs.sdkTypescriptVersion != '' && format('SDK-Typescript {0} ', inputs.sdkTypescriptVersion) }}${{ inputs.sdkJavaVersion != '' && format('SDK-Java {0} ', inputs.sdkJavaVersion) }}${{ inputs.cdkVersion != '' && format('CDK {0} ', inputs.cdkVersion) }}"
7591
add-paths: |
7692
**/package.json
7793
**/package-lock.json
7894
**/build.gradle.kts
7995
**/pom.xml
96+
**/go.mod
97+
**/go.sum

.github/workflows/release.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ jobs:
3232
typescript-bun-hello-world.zip
3333
typescript-cloudflare-worker-hello-world.zip
3434
typescript-deno-hello-world.zip
35+
go-hello-world.zip
3536
kotlin-hello-world-lambda-cdk.zip
3637
java-tour-of-restate.zip
3738
typescript-tour-of-restate.zip

.github/workflows/test.yml

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ name: Build and test
22

33
on:
44
push:
5-
branches: [ main ]
5+
branches: [main]
66
pull_request:
7-
branches: [ main ]
7+
branches: [main]
88

99
jobs:
1010
build-jvm:
@@ -13,15 +13,15 @@ jobs:
1313
runs-on: ubuntu-latest
1414
strategy:
1515
matrix:
16-
jvm-version: [ 17 ]
16+
jvm-version: [17]
1717

1818
steps:
1919
- uses: actions/checkout@v3
2020

2121
- name: Use JVM ${{ matrix.jvm-version }}
2222
uses: actions/setup-java@v4
2323
with:
24-
distribution: 'temurin'
24+
distribution: "temurin"
2525
java-version: ${{ matrix.jvm-version }}
2626

2727
- name: Setup Gradle
@@ -45,18 +45,37 @@ jobs:
4545
uses: actions/setup-node@v3
4646
with:
4747
node-version: ${{ matrix.node-version }}
48-
registry-url: 'https://registry.npmjs.org'
48+
registry-url: "https://registry.npmjs.org"
4949

5050
- name: Run tests
5151
run: ./.tools/run_node_tests.sh
5252

53+
build-go:
54+
# prevent from running on forks
55+
if: github.repository_owner == 'restatedev'
56+
runs-on: ubuntu-latest
57+
strategy:
58+
matrix:
59+
go-version: [1.22]
60+
61+
steps:
62+
- uses: actions/checkout@v3
63+
64+
- name: Use Go ${{ matrix.go-version }}
65+
uses: actions/setup-go@v3
66+
with:
67+
go-version: ${{ matrix.go-version }}
68+
69+
- name: Run tests
70+
run: ./.tools/run_go_tests.sh
71+
5372
build-py:
5473
# prevent from running on forks
5574
if: github.repository_owner == 'restatedev'
5675
runs-on: ubuntu-latest
5776
strategy:
5877
matrix:
59-
python: [ "3.12" ]
78+
python: ["3.12"]
6079

6180
steps:
6281
- uses: actions/checkout@v3

.tools/prepare_release_zip.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ create_release_zip templates/typescript typescript-hello-world
2323
create_release_zip templates/bun typescript-bun-hello-world
2424
create_release_zip templates/cloudflare-worker typescript-cloudflare-worker-hello-world
2525
create_release_zip templates/deno typescript-deno-hello-world
26+
create_release_zip templates/go go-hello-world
2627
create_release_zip templates/typescript-lambda-cdk typescript-hello-world-lambda-cdk
2728
create_release_zip templates/kotlin-gradle-lambda-cdk kotlin-hello-world-lambda-cdk
2829
create_release_zip templates/python python-hello-world

.tools/run_go_tests.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env bash
2+
3+
SELF_PATH=${BASH_SOURCE[0]:-"$(command -v -- "$0")"}
4+
PROJECT_ROOT="$(dirname "$SELF_PATH")/.."
5+
6+
function go_build_check() {
7+
pushd $1
8+
go build -o /dev/null .
9+
go vet ./...
10+
popd
11+
}
12+
13+
go_build_check $PROJECT_ROOT/templates/go

.tools/update_go_examples.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env bash
2+
3+
NEW_VERSION=$1
4+
SELF_PATH=${BASH_SOURCE[0]:-"$(command -v -- "$0")"}
5+
PROJECT_ROOT="$(dirname "$SELF_PATH")/.."
6+
7+
function bump_go_sdk() {
8+
pushd $1
9+
go get github.com/restatedev/sdk-go@$NEW_VERSION
10+
go mod tidy
11+
popd
12+
}
13+
14+
bump_go_sdk $PROJECT_ROOT/templates/go

templates/go/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
go

templates/go/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Hello world - Go example
2+
3+
Sample project configuration of a Restate service using the Go SDK.
4+
5+
You can run locally with `go run .` and register to Restate with
6+
`restate dep add http://localhost:9080`. Then you can invoke with `curl localhost:8080/Greeter/Greet --json '"hello"'`.
7+
8+
You can build a docker image using [ko](https://github.com/ko-build/ko):
9+
`ko build --platform=all`

templates/go/go.mod

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module github.com/restatedev/examples/templates/go
2+
3+
go 1.22.5
4+
5+
require github.com/restatedev/sdk-go v0.9.1
6+
7+
require (
8+
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
9+
github.com/google/uuid v1.6.0 // indirect
10+
github.com/mr-tron/base58 v1.2.0 // indirect
11+
golang.org/x/net v0.21.0 // indirect
12+
golang.org/x/text v0.14.0 // indirect
13+
google.golang.org/protobuf v1.32.0 // indirect
14+
)

templates/go/go.sum

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
4+
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
5+
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
6+
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
7+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
8+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
9+
github.com/mr-tron/base58 v1.2.0 h1:T/HDJBh4ZCPbU39/+c3rRvE0uKBQlU27+QI8LJ4t64o=
10+
github.com/mr-tron/base58 v1.2.0/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc=
11+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
12+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
13+
github.com/restatedev/sdk-go v0.9.1 h1:IWn+f3t8ow996U47nnMcFQwP7Yn8X35IAIdKHHZC504=
14+
github.com/restatedev/sdk-go v0.9.1/go.mod h1:wv6BuURHPiFOe5lomBGA6VETSWvAv8iLIKWBY7JUFSU=
15+
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
16+
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
17+
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
18+
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
19+
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
20+
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
21+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
22+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
23+
google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I=
24+
google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
25+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
26+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

templates/go/greeter.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
restate "github.com/restatedev/sdk-go"
7+
)
8+
9+
// Greeter is a struct which represents a Restate service; reflection will turn exported methods into service handlers
10+
type Greeter struct{}
11+
12+
func (Greeter) Greet(ctx restate.Context, greeting string) (string, error) {
13+
return fmt.Sprintf("%s!", greeting), nil
14+
}

templates/go/main.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"log/slog"
6+
"os"
7+
8+
restate "github.com/restatedev/sdk-go"
9+
"github.com/restatedev/sdk-go/server"
10+
)
11+
12+
func main() {
13+
server := server.NewRestate().
14+
Bind(restate.Service(Greeter{}))
15+
16+
if err := server.Start(context.Background(), ":9080"); err != nil {
17+
slog.Error("application exited unexpectedly", "err", err.Error())
18+
os.Exit(1)
19+
}
20+
}

0 commit comments

Comments
 (0)