Q: How does Golang accept uncertain values Back-end return values are not fixed, sometimes:
{"application": {"instance": [{"instanceId": "v1"}, {"instanceId": "v2"}]}}

or sometimes:
{"application": {"instance": {"instanceId": "v"}}}

how should I take out the corresponding instanceId value?
package main

import (
    "encoding/json"
    "fmt"
)

type Application struct {
    Application struct {
        Instance json.RawMessage `json:"instance"`
    } `json:"application"`
}


func main() {
    a := `{"application": {"instance": {"instanceId": "v"}}}`
    //a := `{"application": {"instance": [{"instanceId": "v1"}, {"instanceId": "v2"}]}} `
    var p Application
    errJson := json.Unmarshal([]byte(a), &p)
    if errJson != nil {
        fmt.Printf("errJson")
    }
    fmt.Printf("type:%T", p.Application.Instance)

}


A: Since the 2 value types clash (one a struct another a slice of structs) it gets messy to encapsulate this into a single type even using catch-all solutions like interface{}.
The simplest solution is present two distinct types and marshal into either to see which "works":
func unmarsh(body []byte) (*type1, *type2, error) {
    var (
        t1 type1
        t2 type2
    )

    err := json.Unmarshal(body, &t1)
    if err == nil {
        return &t1, nil, nil
    }

    err = json.Unmarshal(body, &t2)
    if err == nil {
        return nil, &t2, nil
    }

    return nil, nil, err

}

and in your example the two types would be:
type type1 struct {
    Application struct {
        Instance []struct {
            InstanceID string `json:"instanceId"`
        } `json:"instance"`
    } `json:"application"`
}

type type2 struct {
    Application struct {
        Instance struct {
            InstanceID string `json:"instanceId"`
        } `json:"instance"`
    } `json:"application"`
}

Working example:
https://play.golang.org/p/Kma32gWfghb

A: A cleaner solution would be a custom unmarshaler:
type Instances []Instance

func (i *Instances) UnmarshalJSON(in []byte) error {
   if len(in)>0 && in[0]=='[' {
     var a []Instance
     if err:=json.Unmarshal(in,&a); err!=nil {
        return err
     }
     *i=a
     return nil
  }
  var s Instance
  if err:=json.Unmarshal(in,&s) ; err!=nil {
     return err
  }
  *i=[]Instance{s}
  return nil
}

This would unmarshal an object into a slice of 1.
A more compact solution is provided by @mkopriva:
func (i *Instances) UnmarshalJSON(in []byte) error {
    if len(in) > 0 && in[0] == '[' {
        return json.Unmarshal(in, (*[]Instance)(i))
    }

    *i = Instances{{}}
    return json.Unmarshal(in, &(*i)[0])
}

