lundi 11 juin 2018

Golang: What to return when building an SDK to interact with a Web API?

I'm building an SDK for a web application using Go.

I was wondering what the best form of returning data to users of the SDK would be.

For example, below is a function that takes in an http.Request object. What is the preferred object/struct that should be returned from this function?

func runRequest(request *http.Request) map[string]interface{} {
    resp, err := http.DefaultClient.Do(request)
    //handle error
    defer resp.Body.Close()

    body, err := ioutil.ReadAll(resp.Body)
    var data map[string]interface{}
    err2 := json.Unmarshal([]byte(body), &data)
    //handle error

    return data
}

Aucun commentaire:

Enregistrer un commentaire