Sobes.tech
Middle+

How to correctly store the host URL of an external service?

sobes.tech AI

Answer from AI

It is better to store the external service host URL in configuration files or environment variables rather than hardcoding it in the code. This allows for easy changes to the address without recompilation and simplifies deployment in different environments (dev, staging, prod).

Example of using an environment variable in Go:

package main

import (
    "fmt"
    "os"
)

func main() {
    hostURL := os.Getenv("EXTERNAL_SERVICE_URL")
    if hostURL == "" {
        hostURL = "https://default.service.com" // default value
    }
    fmt.Println("External service host URL:", hostURL)
}

This approach improves the flexibility and security of the application.