Skip to content

Commit 9870254

Browse files
authored
Merge pull request #15 from pcoet/go
initial setup for Go examples
2 parents 448fa24 + d1e5db0 commit 9870254

File tree

5 files changed

+92
-0
lines changed

5 files changed

+92
-0
lines changed

go/README.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Go examples
2+
3+
This directory contains examples of working with the Gemini API using the
4+
[Google Gen AI SDK for Go](https://pkg.go.dev/google.golang.org/genai).
5+
6+
## Run tests
7+
8+
go test

go/go.mod

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module gemini-api-examples
2+
3+
go 1.23
4+
5+
toolchain go1.24.1
6+
7+
require google.golang.org/genai v0.5.0
8+
9+
require (
10+
cloud.google.com/go v0.116.0 // indirect
11+
cloud.google.com/go/compute/metadata v0.5.0 // indirect
12+
github.com/google/go-cmp v0.6.0 // indirect
13+
github.com/gorilla/websocket v1.5.3 // indirect
14+
golang.org/x/oauth2 v0.23.0 // indirect
15+
golang.org/x/sys v0.25.0 // indirect
16+
)

go/go.sum

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
cloud.google.com/go v0.116.0 h1:B3fRrSDkLRt5qSHWe40ERJvhvnQwdZiHu0bJOpldweE=
2+
cloud.google.com/go v0.116.0/go.mod h1:cEPSRWPzZEswwdr9BxE6ChEn01dWlTaF05LiC2Xs70U=
3+
cloud.google.com/go/compute/metadata v0.5.0 h1:Zr0eK8JbFv6+Wi4ilXAR8FJ3wyNdpxHKJNPos6LTZOY=
4+
cloud.google.com/go/compute/metadata v0.5.0/go.mod h1:aHnloV2TPI38yx4s9+wAZhHykWvVCfu7hQbF+9CWoiY=
5+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
6+
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
7+
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
8+
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
9+
golang.org/x/oauth2 v0.23.0 h1:PbgcYx2W7i4LvjJWEbf0ngHV6qJYr86PkAV3bXdLEbs=
10+
golang.org/x/oauth2 v0.23.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
11+
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
12+
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
13+
google.golang.org/genai v0.5.0 h1:0Gg795HqLJ+fBisumETTV6qsIPWBXNqTGVdKAAenhcc=
14+
google.golang.org/genai v0.5.0/go.mod h1:yPyKKBezIg2rqZziLhHQ5CD62HWr7sLDLc2PDzdrNVs=

go/text_generation.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package examples
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"os"
8+
9+
"google.golang.org/genai"
10+
)
11+
12+
func GenerateContentTextOnly() (*genai.GenerateContentResponse, error) {
13+
// [START text_gen_text_only_prompt]
14+
ctx := context.Background()
15+
client, err := genai.NewClient(ctx, &genai.ClientConfig{
16+
APIKey: os.Getenv("GEMINI_API_KEY"),
17+
Backend: genai.BackendGeminiAPI,
18+
})
19+
if err != nil {
20+
log.Fatal(err)
21+
}
22+
23+
result, err := client.Models.GenerateContent(
24+
ctx,
25+
"gemini-2.0-flash",
26+
genai.Text("Write a story about a magic backpack."),
27+
nil,
28+
)
29+
printResponse(result)
30+
// [END text_gen_text_only_prompt]
31+
return result, err
32+
}
33+
34+
func printResponse(resp *genai.GenerateContentResponse) {
35+
for _, cand := range resp.Candidates {
36+
if cand.Content != nil {
37+
for _, part := range cand.Content.Parts {
38+
fmt.Println(part.Text)
39+
}
40+
}
41+
}
42+
}

go/text_generation_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package examples
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestGenerateContentTextOnly(t *testing.T) {
8+
_, err := GenerateContentTextOnly()
9+
if err != nil {
10+
t.Errorf("GenerateContentTextOnly returned an error.")
11+
}
12+
}

0 commit comments

Comments
 (0)