Похожие чаты

We can't have a generic value for a map right? Suppose

I have these JSON models:
{
"type": "signup",
"data": {username: "", "password": ""}
}
{
"type": "new_post",
"data": {"title": "", "description": ""}
}

type Event struct {
Type string `json:"type"`
Data json.RawMessage `json:"data"`
}

type Signup struct {
Username string `json:"username"`
Password string `json:"password"`
}

type NewPost struct {
Title string `json:"title"`
Description string `json:"description"`
}

Now I have these functions:

func HandleSignup(signup *Signup) {}
func HandleNewPost(newPost *NewPost) {}

What I need is a map to store these functions that receive different types, then have another function to act based on event type and decode the json.RawMessage to the respective`Type`and pass it to the respected handler.

4 ответов

7 просмотров
ㅤ- Автор вопроса

I love reflection but the company doesn't :D

how about using a code generator like go-swagger?

ㅤ- Автор вопроса
Roman Sharkov
how about using a code generator like go-swagger?

I'll check it out, haven't worked with generators

Похожие вопросы

Обсуждают сегодня

Hi everyone! I have a two-folded message to share today. First: updates and news, as we are being asked for those. We still want to go like we did the last 6 years: we w...
Marten | Unibright.io
31
Oh.... Admins at the main TG is already muted me. How many months Sir you muted me? Or forever if you are here @Janevietani @JohnnySonic
Charles Dar
28
Скажите, можно ли как-то "переместить" динамический массив из одной переменной в другую? Скажем, переместить из TList<> в TArray<>. Именно переместить, а не скопировать. Если ...
Eugene Krasnikov (ᴊɪɴ x)
37
комрады, че-та лыжы не едут var tmpFont: TFont; begin tmpFont:= TFont.Create; try case rgFontColor.ItemIndex of 0: tmpFont.Color:= clWindowText; 1: tmpFo...
Ed Doc
34
.model small .stack 100h .data a db 'Hello, World!', '$' ; исходная строка b db 20 dup(?) ; строка b с запасом на максимальную длину .code main: ...
Алексей -man
3
вопрос, кого посмотреть в ютубе или где почитать про указатели чтобы раз и навсегда запомнить зачем они нужны и как правильно ими пользоваться? поделитесь хорошими ресурсами, ...
-
14
М-да. Почему бы просто со stringlist не работать?
Michael Longneck
23
Интересно, нет ли какого-то способа получить из dll не адрес самой метки, а адрес со смещением?
The Bird of Hermes
54
Редактор листа Excel, по сути двумерный массив ячеек. Ячейка - это экземпляр класса, у нее всякие свойства, методы. Проблема в том, что количество используемых строк и колоно...
Sergey Bodrov
2
in one table i have two columns one is unique and another is duplicate. unique always unique and duplicate is that maybe has duplicate and may be not. now i want get Duplicat...
Mr Thieves
2
Карта сайта