Use environment variables instead of hardcoding configuration values such as file paths, port numbers, or system-specific locations. This makes the code more flexible across different platforms, installation methods, and user preferences.
When implementing configuration:
~
expansion) across operating systemsExample:
// Bad: Hardcoded path that won't work across environments
func RegisterService() {
server, err = zeroconf.Register(
"OllamaInstance", // Service Name
"_ollama._tcp", // Service Type
"local.", // Domain
11434, // Port - hardcoded!
)
}
// Good: Use environment variable with fallback
func RegisterService() {
port := 11434 // Default
if hostEnv := os.Getenv("OLLAMA_HOST"); hostEnv != "" {
if _, portStr, err := net.SplitHostPort(hostEnv); err == nil && portStr != "" {
if p, err := strconv.Atoi(portStr); err == nil {
port = p
}
}
}
server, err = zeroconf.Register(
"OllamaInstance", // Service Name
"_ollama._tcp", // Service Type
"local.", // Domain
port, // Port from environment or default
)
}
For path handling, support platform-specific conventions:
func getConfigPath() string {
if path, exists := os.LookupEnv("OLLAMA_CONFIG"); exists {
// Handle home directory expansion
if strings.HasPrefix(path, "~/") {
homeDir, err := os.UserHomeDir()
if err == nil {
path = filepath.Join(homeDir, path[2:])
}
}
return path
}
// Default fallback
homeDir, _ := os.UserHomeDir()
return filepath.Join(homeDir, ".ollama", "config")
}
Enter the URL of a public GitHub repository