Clever way to pick and use correct service


This is more like a question in OOP strategy pattern so you pass a flag to parent service then it cleverly picks and uses the relevant sub service to do the job so you only call the parent service.

In example below, I am currently initialising two clients with different parameters and pass them both to another package to use the correct one based on a flag/var. However, as you can see, it is not nice and not scalable. If I had 10 environments I would end up passing 10 clients. The problem is, I only know about the client parameters at boot time hence reason I initialise clients at application build only once.

What would be the way to pass just one client instead?



package main

import (

func main() {

	live := client.New("http://prod", "prod", "000")
	stag := client.New("http://stag", "stag", "111")

	consumer.Consume(live, stag)


package client

type Client struct {
	url    string
	key    string
	secret string

func New(url, key, secret string) Client {
	return Client{
		url:    url,
		key:    key,
		secret: secret,

func (c Client) Connect() {
	// Use c.url, c.key and c.secret to connect to some external service.


package consumer

import (

func Consume(live client.Client, stag client.Client) {
	// Assume that this is a user input.
	// I am just hard-coding it for demonstration purposes.
	env := "prod"

	if env == "prod" {
		fmt.Println("consumed prod")
	} else {
		fmt.Println("consumed stag")

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.