フラッシュライブがゲームの裏で落ちまくるのでどうしたらいいかとジェミニさんに聞くと
10行でできるからとやってみた結果1日かかってます。
しかもできるかわからないほど難易度高い様子。でも会話できるまでにはなったので
アップロードします。
またGo言語は無料でできたC言語のようなものなのでいいねという感じです。
メモ帳とシェルコマンドラインでできます。
go get google.golang.org/genai
と打ちSDKを入れる
main.go の中身
package main
import (
“bufio”
“context”
“fmt”
“log”
“os”
"google.golang.org/genai"
)
func main() {
ctx := context.Background()
client, err := genai.NewClient(ctx, &genai.ClientConfig{
APIKey: os.Getenv(“GEMINI_API_KEY”),
})
if err != nil {
log.Fatal(err)
}
modelID := "gemini-3-flash-preview"
// 履歴を保持するスライス
var history []*genai.Content
systemInstruction := &genai.Content{
Parts: []*genai.Part{{Text: "あなたは誠実なパートナーです。1o2o3o4o5o チェックを忘れずに。"}},
}
scanner := bufio.NewScanner(os.Stdin)
fmt.Println("--- Gemini 3-Flash 物理対話モード (exitで終了) ---")
for {
fmt.Print("YOU > ")
if !scanner.Scan() { break }
input := scanner.Text()
if input == "exit" { break }
// ユーザーの発言を構造化
userContent := &genai.Content{
Role: "user",
Parts: []*genai.Part{{Text: input}},
}
// 重要:今回の入力を履歴に追加
history = append(history, userContent)
config := &genai.GenerateContentConfig{
SystemInstruction: systemInstruction,
}
// 物理的修正:history (スライス) をそのまま渡す
resp, err := client.Models.GenerateContent(ctx, modelID, history, config)
if err != nil {
log.Printf("Error: %v", err)
continue
}
if len(resp.Candidates) > 0 && len(resp.Candidates[0].Content.Parts) > 0 {
aiText := resp.Candidates[0].Content.Parts[0].Text
fmt.Println("AI >", aiText)
// モデルの回答も履歴に追加して次回の文脈を作る
modelContent := &genai.Content{
Role: "model",
Parts: []*genai.Part{{Text: aiText}},
}
history = append(history, modelContent)
}
}
}