How to make a struct that can accept every type

Hey Guys,
I want to make a Response that returns from function inside database.go be customizable.
for example, in the domain file, I have this Function:
UserLogin(userCredential Credentials, password string) (*Response, error)

I want to return for response of this function a
token: "user-generated-token".

and in another function I want to return as response a message like this:
RegisterUser(input User) (*Response, error)
message: "User created"

and in another function that returns from database function return a JSON object or JSON array.
and then put that response from database.go into this function for payload section:

func RespondJSON(w http.ResponseWriter, status int, result bool, message string, payload interface{}) {
	response, err := json.Marshal(&domain.BaseEndPoint{
		Success:   result,
		Message:  message,
		Response: payload,
	if err != nil {
	w.Header().Set("Content-Type", "application/json")

It’s not a logical a make a struct per any response. how I can make the structure that can accept any type and key-value. this is my struct right now:

type Response struct {
Result interface{} `json:"result"`


and result in postman:

     "success": true,
    "message ": "welcome .....",
    "response": {
        "result": {
            "mobile": "+1 123456",
            "password": "123456"

I need a way to remove just result in this case and then everything is ok.

PS: is it ok if I use in the domain for the result?


but I think I got a problem when I need to return the list of products or users. []map won’t work. is it correct?

or may we don’t need a struct with Response name? and just in the domain make return type that we need?

Can you clarify what you’re asking?

@mehrdaddolatkhah I’m not 100% sure about your question. But, you can just use an Empty interface instead of a struct with empty interface inside it.

var Response interface{}

You can use that instead of

type Response struct {
Result interface{} json:"result"

Hope this answers your question.


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