The Go client allows you to configure which server to target using context values. By default, it uses the first server defined in the OpenAPI specification.
Select a specific server index
To use a server other than the one at index 0, set the client.ContextServerIndex value in your context.
Configure templated server variables
You can provide variables (like basePath) for templated URLs by setting client.ContextServerVariables in your context.
Per-operation URL overrides
You can override the server URL for a specific operation using the OperationServers map in the Configuration. Operations are identified by the string format "{classname}Service.{nickname}". You can override these via context using:
client.ContextOperationServerIndices: A map of operation identifiers to server indices.client.ContextOperationServerVariables: A map of operation identifiers to variable maps.
// Select server index
ctx := context.WithValue(context.Background(), client.ContextServerIndex, 1)
// Set templated variables
ctx := context.WithValue(context.Background(), client.ContextServerVariables, map[string]string{
"basePath": "v2",
})
// Override specific operation server index
ctx := context.WithValue(context.Background(), client.ContextOperationServerIndices, map[string]int{
"{classname}Service.{nickname}": 2,
})
// Override specific operation server variables
ctx = context.WithValue(context.Background(), client.ContextOperationServerVariables, map[string]map[string]string{
"{classname}Service.{nickname}": {
"port": "8443",
},
})