英文:
Solution for Generic in Go
问题
我想为JSON响应创建一个有用的库。在Java中,我已经有了这个。我现在开始使用Go,不知道如何转换我的Java代码。我已经读到Go没有泛型这样的东西,但是我该如何解决我的问题呢?
我在谈论以下代码的部分:
@Data
public class ServiceResult<T extends Serializable> implements Serializable {
private ServiceResultStatus status;
private String type;
private T content;
private String hash;
private String destination;
private HashMap<String, Metadata> metadata = new HashMap<>();
...
service-result的想法是为RESTful web服务提供一个结构化模式。如果你需要更多信息,这是我在Github上的存储库链接:https://github.com/Viascom/service-result
service-result最终的样子是这样的:
{
"status": "successful",
"type": "ch.viascom.example.models.response.GetTasksResponse",
"content": [
{
"id": "3e99c7fb-0ed7-11e7-a7a5-0050569c3e5a",
"name": "Example Task"
}
],
"hash": "7bf9c04d1e9f8fe7995e4b8beeac1a4c830e7ea",
"destination": "ch.viascom.example.handler.TaskHandler",
"metadata": {
}
}
英文:
I want to make a useful library for JSON responses. In Java I've this already. I started now new with Go and have no idea how to transform my Java code. I've read that Go doesn't have anything like generics, but how can I solve my problem?
I'm talking about the following part of code:
@Data
public class ServiceResult<T extends Serializable> implements Serializable {
private ServiceResultStatus status;
private String type;
private T content;
private String hash;
private String destination;
private HashMap<String, Metadata> metadata = new HashMap<>();
...
The idea of service-result is to provide an structural pattern for RESTful web services. If you need more information, here is the link to my repo on Github: https://github.com/Viascom/service-result
A service-result looks at the end like this:
{
"status": "successful",
"type": "ch.viascom.example.models.response.GetTasksResponse",
"content": [
{
"id": "3e99c7fb-0ed7-11e7-a7a5-0050569c3e5a",
"name": "Example Task"
}
],
"hash": "7bf9c04d1e9f8fe7995e4b8beeac1a4c830e7ea",
"destination": "ch.viascom.example.handler.TaskHandler",
"metadata": {
}
}
答案1
得分: 2
你可以直接将json映射添加到结构定义中,并使用编码器和解码器进行编组和解组。这一切都是内置的,比其他语言更容易,我个人认为。
以下是来自playground的一个很好的示例:https://play.golang.org/p/4L2wMVv7tW
对于你的特定情况,应该是这样的:
type ServiceResult struct {
Status ServiceResultStatus `json:"status"`
Type string `json:"type"`
Hash string `json:"hash"`
Destination string `json:"destination"`
Metadata map[string]Metadata `json:"metadata"`
}
type ExplizitServiceResult struct {
ServiceResult
Content SomeStruct `json:"content"`
}
https://play.golang.org/p/FFfiq6LxVt
如果你不想从ServiceResult派生每个用户结构体,你可以将content定义为interface{}
,这样每个结构体都可以插入。我已经更新了我的示例以解决这个问题。也许这是你问题的最简单解决方案。
https://play.golang.org/p/LNgreqrnnw
英文:
You can add the json-mapping directly to the structure definition and use the encoder, decoder to marshal and unmarshal it. It's all built in and easier than in other languages, imho.
type ServiceResponse struct {
Value string`json:"nameInJsonResponse"`
}
here is a good example from the playground: https://play.golang.org/p/4L2wMVv7tW
For your particular case it should be something like this:
type ServiceResult struct {
Status ServiceResultStatus`json:"status"`
Type string`json:"type"`
Hash string`json:"hash"`
Destination string`json:"destination"`
Metadata map[string]Metadata metadata`json:"metadata"`
}
type ExplizitServiceResult struct {
ServiceResult
Content SomeStruct`json:"content"`
}
https://play.golang.org/p/FFfiq6LxVt
If you don't want to derive every user struct from the ServiceResult you can define the content as interface{}
so every struct can be inserted. I've updated my example for this. Maybe this is the easiest solution to your problem.
https://play.golang.org/p/LNgreqrnnw
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论