Go中的泛型解决方案

huangapple go评论93阅读模式
英文:

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&lt;T extends Serializable&gt; implements Serializable {

    private ServiceResultStatus status;
    private String type;
    private T content;
    private String hash;
    private String destination;
    private HashMap&lt;String, Metadata&gt; metadata = new HashMap&lt;&gt;();

...

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:

{
  &quot;status&quot;: &quot;successful&quot;,
  &quot;type&quot;: &quot;ch.viascom.example.models.response.GetTasksResponse&quot;,
  &quot;content&quot;: [
    {
      &quot;id&quot;: &quot;3e99c7fb-0ed7-11e7-a7a5-0050569c3e5a&quot;,
      &quot;name&quot;: &quot;Example Task&quot;
    }
  ],
  &quot;hash&quot;: &quot;7bf9c04d1e9f8fe7995e4b8beeac1a4c830e7ea&quot;,
  &quot;destination&quot;: &quot;ch.viascom.example.handler.TaskHandler&quot;,
  &quot;metadata&quot;: {
  
  }
}

答案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:&quot;nameInJsonResponse&quot;`
}

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:&quot;status&quot;`
	Type string`json:&quot;type&quot;`
	Hash string`json:&quot;hash&quot;`
	Destination string`json:&quot;destination&quot;`
	Metadata map[string]Metadata metadata`json:&quot;metadata&quot;`
}

type ExplizitServiceResult struct {
    ServiceResult
    Content SomeStruct`json:&quot;content&quot;`
}

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

huangapple
  • 本文由 发表于 2017年8月4日 15:34:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/45500781.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定