go get github.com/rizome-dev/go-claude-codebuilt by: rizome labs
contact us: hi (at) rizome.dev
package main
import (
"context"
"fmt"
"log"
"github.com/rizome-dev/go-claude-code/pkg"
)
func main() {
ctx := context.Background()
response, err := pkg.SimpleQuery(ctx, "What is the capital of France?")
if err != nil {
log.Fatal(err)
}
fmt.Println(response)
}package main
import (
"context"
"fmt"
"log"
"github.com/rizome-dev/go-claude-code/pkg"
)
func main() {
ctx := context.Background()
client, err := pkg.NewClient(ctx, nil)
if err != nil {
log.Fatal(err)
}
defer client.Close()
// Send a message
err = client.SendMessage(ctx, "Explain Go concurrency")
if err != nil {
log.Fatal(err)
}
// Stream responses
for msg := range client.StreamMessages(ctx) {
switch m := msg.(type) {
case *pkg.AssistantMessage:
for _, block := range m.Content {
if text, ok := block.(pkg.TextBlock); ok {
fmt.Print(text.Text)
}
}
case pkg.ResultMessage:
fmt.Printf("\nCost: $%.4f\n", m.Data.Cost.TotalCost)
return
}
}
}Sends a simple query and returns the text response.
Sends a query with options and returns detailed results including messages and metadata.
Sends a query with a configuration function for setting options.
Creates a new interactive client with the specified options.
Sends a user message to Claude.
Sends an interrupt signal to stop Claude's current response.
Returns a channel that streams all messages from Claude.
Blocks until a result message is received.
Closes the client and cleans up resources.
UserMessage: User input messagesAssistantMessage: Claude's responses with content blocksSystemMessage: System-level messages (usage, thinking, errors)ResultMessage: Final result with usage and cost information
TextBlock: Plain text contentToolUseBlock: Tool invocation detailsToolResultBlock: Tool execution results
type ClaudeCodeOptions struct {
ApiKeyName string
BaseURL string
Model string
MaxTokens int
MaxBackgroundTokens int
MaxCostUSD float64
Temperature float64
CustomInstructions string
SystemPrompt string
Mode PermissionMode
AssistantID string
OnlyTools []string
McpServers map[string]MCPServerConfig
MaxFileUploadsBytes int
MaxImagePixels int
Cwd string
SessionID string
}options := &pkg.ClaudeCodeOptions{
McpServers: map[string]pkg.MCPServerConfig{
"filesystem": {
Type: pkg.MCPServerTypeStdio,
Command: "mcp-server-filesystem",
Args: []string{"/workspace"},
},
"api": {
Type: pkg.MCPServerTypeHTTP,
URL: "https://api.example.com/mcp",
APIKey: "your-api-key",
},
},
}The SDK provides specific error types for different scenarios:
result, err := pkg.Query(ctx, prompt, options)
if err != nil {
var cliNotFound *errors.CLINotFoundError
if errors.As(err, &cliNotFound) {
// Claude Code CLI is not installed
fmt.Println("Please install Claude Code CLI")
return
}
var procErr *errors.ProcessError
if errors.As(err, &procErr) {
// CLI process failed
fmt.Printf("Process failed with code %d: %s\n",
procErr.ExitCode, procErr.Stderr)
return
}
// Handle other errors
log.Fatal(err)
}ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
result, err := pkg.Query(ctx, "Complex task...", options)
if err == context.DeadlineExceeded {
fmt.Println("Query timed out")
}