How do I change the target_url for my proxy in a Custom Go Plugin?

This works using the UI and an advanced trigger via URL Rewrite. But in my Go plugin, I want to change the target_url so that I can do header validation and process the request to one of multiple upstreams depending on the header value.

Eg. header “UserID: 1” should send the upstream to Service A
header “UserID: 2” should send the upstream to Service B

etc.

The example below will accomplish a target_url redirect based on header value, in a Go plugin.

func setUpstream(r *http.Request, upstream string) {
	apiDef := ctx.GetDefinition(r)
	apiDef.Proxy.EnableLoadBalancing = true
	apiDef.Proxy.StructuredTargetList = apidef.NewHostListFromList([]string{upstream})
}

func ChangeUpstreamURL(rw http.ResponseWriter, r *http.Request) {
	userHeader := r.Header.Get("UserID")

	if userHeader == "1" {
		setUpstream(r, "https://httpbingo.org")
	} else if userHeader == "2" {
		setUpstream(r, "https://httpbin.org")
	} else {
		setUpstream(r, "https://google.com")
	}
}