Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,107 @@ python weather_assistant.py

</details>

<details>
<summary><strong>Go</strong></summary>

Create `weather-assistant.go`:

```go
package main

import (
"bufio"
"fmt"
"log"
"math/rand"
"os"
"strings"

copilot "github.com/github/copilot-sdk/go"
)

type WeatherParams struct {
City string `json:"city" jsonschema:"The city name"`
}

type WeatherResult struct {
City string `json:"city"`
Temperature string `json:"temperature"`
Condition string `json:"condition"`
}

func main() {
getWeather := copilot.DefineTool(
"get_weather",
"Get the current weather for a city",
func(params WeatherParams, inv copilot.ToolInvocation) (WeatherResult, error) {
conditions := []string{"sunny", "cloudy", "rainy", "partly cloudy"}
temp := rand.Intn(30) + 50
condition := conditions[rand.Intn(len(conditions))]
return WeatherResult{
City: params.City,
Temperature: fmt.Sprintf("%d°F", temp),
Condition: condition,
}, nil
},
)

client := copilot.NewClient(nil)
if err := client.Start(); err != nil {
log.Fatal(err)
}
defer client.Stop()

session, err := client.CreateSession(&copilot.SessionConfig{
Model: "gpt-4.1",
Streaming: true,
Tools: []copilot.Tool{getWeather},
})
if err != nil {
log.Fatal(err)
}

session.On(func(event copilot.SessionEvent) {
if event.Type == "assistant.message_delta" {
fmt.Print(*event.Data.DeltaContent)

Copilot AI Feb 2, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

event.Data.DeltaContent is a pointer and may be nil (the Go SDK types mark it as omitempty). Dereferencing it unconditionally can panic at runtime; guard for nil before printing (or handle empty deltas).

Suggested change
fmt.Print(*event.Data.DeltaContent)
if event.Data != nil && event.Data.DeltaContent != nil {
fmt.Print(*event.Data.DeltaContent)
}

Copilot uses AI. Check for mistakes.

@Ota1022 Ota1022 Feb 2, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, though event.Data is a value type (not a pointer), so only DeltaContent needs a nil check.
fix: simplify condition in session event handling for Go example

}
if event.Type == "session.idle" {
fmt.Println()
}
})

fmt.Println("🌤️ Weather Assistant (type 'exit' to quit)")
fmt.Println(" Try: 'What's the weather in Paris?' or 'Compare weather in NYC and LA'\n")

scanner := bufio.NewScanner(os.Stdin)
for {
fmt.Print("You: ")
if !scanner.Scan() {
break
}
Comment on lines +862 to +864

Copilot AI Feb 2, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The loop exits when scanner.Scan() returns false, but the example never checks scanner.Err(). Add an error check after the loop so input/read errors don't get silently swallowed (and so users can distinguish EOF from an actual failure).

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

input := scanner.Text()
if strings.ToLower(input) == "exit" {
break
}

fmt.Print("Assistant: ")
_, err = session.SendAndWait(copilot.MessageOptions{Prompt: input}, 0)
if err != nil {
log.Fatal(err)
}
Comment on lines +872 to +875

Copilot AI Feb 2, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using log.Fatal here will call os.Exit(1) and skip deferred cleanup (e.g., defer client.Stop() above). Prefer returning from main after logging, or explicitly stop/close resources before exiting so the example doesn't leak the underlying process/session on errors.

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Same issue exists in Steps 3–4 as well — can address separately.

fmt.Println()
}
}
```

Run with:

```bash
go run weather-assistant.go
```

</details>

<details>
<summary><strong>.NET</strong></summary>

Expand Down
Loading