This document provides detailed information about Gopher’s internal APIs and data structures.
Represents a Go version with metadata.
type Version struct {
Version string `json:"version"` // e.g., "1.21.0"
OS string `json:"os"` // e.g., "darwin", "linux", "windows"
Arch string `json:"arch"` // e.g., "amd64", "arm64"
InstalledAt time.Time `json:"installed_at"`
IsActive bool `json:"is_active"`
IsSystem bool `json:"is_system"` // true if system installation
Path string `json:"path"` // path to go binary
}
Methods:
String() string - Basic string representationFullString() string - Detailed string representationIsCompatible() bool - Check if compatible with current systemInformation about available Go versions.
type VersionInfo struct {
Version string `json:"version"`
Stable bool `json:"stable"`
ReleaseDate string `json:"release_date"`
Files []File `json:"files"`
}
Downloadable file information.
type File struct {
Filename string `json:"filename"`
OS string `json:"os"`
Arch string `json:"arch"`
Size int64 `json:"size"`
SHA256 string `json:"sha256"`
}
Detailed system Go information.
type SystemGoInfo struct {
Path string `json:"path"`
Version string `json:"version"`
GOROOT string `json:"goroot"`
GOPATH string `json:"gopath"`
InstalledAt time.Time `json:"installed_at"`
IsSystem bool `json:"is_system"`
}
Gopher configuration.
type Config struct {
InstallDir string `json:"install_dir"`
DownloadDir string `json:"download_dir"`
MirrorURL string `json:"mirror_url"`
AutoCleanup bool `json:"auto_cleanup"`
MaxVersions int `json:"max_versions"`
}
Main interface for version management operations.
type ManagerInterface interface {
// ListInstalled returns all installed Go versions
ListInstalled() ([]Version, error)
// ListAvailable returns all available Go versions
ListAvailable() ([]VersionInfo, error)
// Install downloads and installs a Go version
Install(version string) error
// Uninstall removes a Go version
Uninstall(version string) error
// Use switches to a Go version
Use(version string) error
// GetCurrent returns the currently active Go version
GetCurrent() (*Version, error)
// IsInstalled checks if a version is installed
IsInstalled(version string) (bool, error)
}
Main manager implementation.
type Manager struct {
config *config.Config
downloader *downloader.Downloader
installer *installer.Installer
systemDetector *SystemDetector
}
func NewManager(cfg *config.Config) *Manager
Creates a new manager with the provided configuration.
func (m *Manager) ListInstalled() ([]Version, error)
Returns all installed Go versions, including system versions.
Returns:
[]Version - List of installed versionserror - Any error that occurredExample:
versions, err := manager.ListInstalled()
if err != nil {
log.Fatal(err)
}
for _, v := range versions {
fmt.Printf("Version: %s, System: %v\n", v.Version, v.IsSystem)
}
func (m *Manager) Install(version string) error
Downloads and installs a specific Go version.
Parameters:
version - Go version to install (e.g., “1.21.0”, “go1.21.0”)Returns:
error - Any error that occurredExample:
err := manager.Install("1.21.0")
if err != nil {
log.Fatal(err)
}
func (m *Manager) Use(version string) error
Switches to a specific Go version.
Parameters:
version - Go version to use, or “system” for system GoReturns:
error - Any error that occurredExample:
// Switch to specific version
err := manager.Use("1.21.0")
if err != nil {
log.Fatal(err)
}
// Switch to system Go
err = manager.Use("system")
if err != nil {
log.Fatal(err)
}
func (m *Manager) GetCurrent() (*Version, error)
Returns the currently active Go version.
Returns:
*Version - Current version informationerror - Any error that occurredExample:
current, err := manager.GetCurrent()
if err != nil {
log.Fatal(err)
}
fmt.Printf("Current version: %s\n", current.Version)
func (m *Manager) IsInstalled(version string) (bool, error)
Checks if a version is installed.
Parameters:
version - Go version to check, or “system” for system GoReturns:
bool - True if installederror - Any error that occurredExample:
installed, err := manager.IsInstalled("1.21.0")
if err != nil {
log.Fatal(err)
}
if installed {
fmt.Println("Go 1.21.0 is installed")
}
func (m *Manager) GetSystemInfo() (*SystemGoInfo, error)
Returns detailed information about system Go.
Returns:
*SystemGoInfo - System Go informationerror - Any error that occurredExample:
info, err := manager.GetSystemInfo()
if err != nil {
log.Fatal(err)
}
fmt.Printf("System Go: %s\n", info.Version)
Handles detection of system-installed Go versions.
type SystemDetector struct{}
func NewSystemDetector() *SystemDetector
Creates a new system detector.
func (sd *SystemDetector) DetectSystemGo() (*Version, error)
Detects the system-installed Go version.
Returns:
*Version - System Go version informationerror - Any error that occurredExample:
detector := NewSystemDetector()
version, err := detector.DetectSystemGo()
if err != nil {
log.Fatal(err)
}
fmt.Printf("System Go: %s\n", version.Version)
func (sd *SystemDetector) IsSystemGoAvailable() bool
Checks if system Go is available.
Returns:
bool - True if system Go is availableExample:
detector := NewSystemDetector()
if detector.IsSystemGoAvailable() {
fmt.Println("System Go is available")
}
func (sd *SystemDetector) GetSystemGoPath() (string, error)
Returns the path to the system Go binary.
Returns:
string - Path to go binaryerror - Any error that occurredExample:
path, err := detector.GetSystemGoPath()
if err != nil {
log.Fatal(err)
}
fmt.Printf("System Go path: %s\n", path)
func (sd *SystemDetector) GetSystemGoInfo() (*SystemGoInfo, error)
Returns detailed system Go information.
Returns:
*SystemGoInfo - Detailed system Go informationerror - Any error that occurredExample:
info, err := detector.GetSystemGoInfo()
if err != nil {
log.Fatal(err)
}
fmt.Printf("GOROOT: %s\n", info.GOROOT)
Configuration management.
type Config struct {
InstallDir string `json:"install_dir"`
DownloadDir string `json:"download_dir"`
MirrorURL string `json:"mirror_url"`
AutoCleanup bool `json:"auto_cleanup"`
MaxVersions int `json:"max_versions"`
}
func DefaultConfig() *Config
Returns the default configuration.
func Load(configPath string) (*Config, error)
Loads configuration from file.
Parameters:
configPath - Path to configuration fileReturns:
*Config - Loaded configurationerror - Any error that occurredExample:
config, err := config.Load("~/.gopher/config.json")
if err != nil {
log.Fatal(err)
}
func (c *Config) Save(configPath string) error
Saves configuration to file.
Parameters:
configPath - Path to save configurationReturns:
error - Any error that occurredExample:
err := config.Save("~/.gopher/config.json")
if err != nil {
log.Fatal(err)
}
func (c *Config) Validate() error
Validates the configuration.
Returns:
error - Any validation errorsExample:
err := config.Validate()
if err != nil {
log.Fatal(err)
}
err := ValidateVersion("invalid")
// Returns: "invalid version format: invalid"
err := manager.Install("1.21.0")
// Possible errors:
// - "version 1.21.0 is already installed"
// - "failed to download version: ..."
// - "failed to install version: ..."
err := manager.Use("system")
// Possible errors:
// - "system Go is not available"
// - "failed to create symlink: permission denied"
// Check for specific error types
if err != nil {
if strings.Contains(err.Error(), "permission denied") {
// Handle permission errors
} else if strings.Contains(err.Error(), "not found") {
// Handle not found errors
} else {
// Handle other errors
}
}
{
"type": "object",
"properties": {
"version": {
"type": "string",
"description": "Go version (e.g., '1.21.0')"
},
"os": {
"type": "string",
"description": "Operating system (e.g., 'darwin', 'linux', 'windows')"
},
"arch": {
"type": "string",
"description": "Architecture (e.g., 'amd64', 'arm64')"
},
"installed_at": {
"type": "string",
"format": "date-time",
"description": "Installation timestamp"
},
"is_active": {
"type": "boolean",
"description": "Whether this version is currently active"
},
"is_system": {
"type": "boolean",
"description": "Whether this is a system installation"
},
"path": {
"type": "string",
"description": "Path to the go binary"
}
},
"required": ["version", "os", "arch", "installed_at", "is_active", "is_system", "path"]
}
{
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "Path to the go binary"
},
"version": {
"type": "string",
"description": "Full go version output"
},
"goroot": {
"type": "string",
"description": "GOROOT environment variable value"
},
"gopath": {
"type": "string",
"description": "GOPATH environment variable value"
},
"installed_at": {
"type": "string",
"format": "date-time",
"description": "Installation timestamp"
},
"is_system": {
"type": "boolean",
"description": "Whether this is a system installation"
}
},
"required": ["path", "version", "goroot", "gopath", "installed_at", "is_system"]
}
package main
import (
"fmt"
"log"
"github.com/molmedoz/gopher/internal/config"
"github.com/molmedoz/gopher/internal/version"
)
func main() {
// Load configuration
cfg, err := config.Load(config.GetConfigPath())
if err != nil {
log.Fatal(err)
}
// Create manager
manager := version.NewManager(cfg)
// List installed versions
versions, err := manager.ListInstalled()
if err != nil {
log.Fatal(err)
}
for _, v := range versions {
fmt.Printf("Version: %s, System: %v\n", v.Version, v.IsSystem)
}
}
package main
import (
"fmt"
"log"
"github.com/molmedoz/gopher/internal/version"
)
func main() {
detector := version.NewSystemDetector()
if detector.IsSystemGoAvailable() {
info, err := detector.GetSystemGoInfo()
if err != nil {
log.Fatal(err)
}
fmt.Printf("System Go: %s\n", info.Version)
fmt.Printf("GOROOT: %s\n", info.GOROOT)
fmt.Printf("GOPATH: %s\n", info.GOPATH)
} else {
fmt.Println("No system Go found")
}
}
package main
import (
"fmt"
"log"
"github.com/molmedoz/gopher/internal/config"
"github.com/molmedoz/gopher/internal/version"
)
func main() {
// Create custom configuration
cfg := &config.Config{
InstallDir: "/opt/go-versions",
DownloadDir: "/tmp/gopher-downloads",
MirrorURL: "https://go.dev/dl/",
AutoCleanup: true,
MaxVersions: 10,
}
// Validate configuration
if err := cfg.Validate(); err != nil {
log.Fatal(err)
}
// Create manager with custom config
manager := version.NewManager(cfg)
// Use manager...
}
This API reference provides comprehensive information about Gopher’s internal APIs. For more examples and usage patterns, see the User Guide.