doulangbizhan5160 2015-09-15 14:01 采纳率: 100%
浏览 75

如何将Java注释转换为代码?

I'm changing my java project to golang. I'm using a java annotation and want to convert it into go code. I want to know the best data structure in go to convert this annotation.

My code is as follows:

package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "io/ioutil"
    "net/http"
    "strconv"
    "time"
)

func getBody(method string, Id string, auth string, body []byte,timeStamp int64) ([]byte, error) {

    url := "****************"+Id+"***********"+strconv.FormatInt(timeStamp,10)
    client := &http.Client{}
    req, err := http.NewRequest(method, url, bytes.NewReader(body))

    if err != nil {
        return nil, err
    }

    var header = make(map[string]string,2)
    header["Accept"]="*******************"
    header["Authorization"]=auth

    for key, value := range header {
        req.Header.Add(key, value)
    }

    res, err := client.Do(req)
    defer res.Body.Close()

    if err != nil {
        return nil, err
    }

    var bodyBytes []byte

    if res.StatusCode == 200 {
        bodyBytes, err = ioutil.ReadAll(res.Body)
    } else if err != nil {
        return nil, err
    } else {
        return nil, fmt.Errorf("The remote end did not return a HTTP 200 (OK) response.")
    }

    return bodyBytes, nil

}

func main() {

    method := "GET"
    auth:="************"
    var timeStamp int64 = 1441689793403
    Id:="***********"
    fmt.Println(timeStamp)
    var cs int64 = time.Now().UnixNano()/1000000
    for{
        cs = time.Now().UnixNano()/1000000
        bodyBytes, err := getBody(method, Id, auth, nil,timeStamp)
        if err != nil {
            fmt.Errorf("unable to retrieve the response body from the Glance API server: %v", err)
        }

        var obj []interface{}
        err = json.Unmarshal(bodyBytes, &obj)

        if err != nil {
            fmt.Errorf("unable to parse the JSON response:", err)
        }

        for i,_:=range obj{
            m := obj[i].(map[string]interface{})
            fmt.Println(m["text"])

         /* @OnKeyWord("hi")
          public void HelloWorld(TeamchatAPI api) {
          System.out.println("Hello word");
                );  */


        }

        timeStamp = cs
    }
}

I want to convert the commented java code into go code.

Alternatively you can take your own example to explain, if you want.

  • 写回答

1条回答 默认 最新

  • douxiangshi6568 2015-11-18 21:17
    关注

    After you have done the translation as @icza explained, you wish probably to move the functionality to a middleware (example) that gets run for all your requests. That way you can add the assisting logic (the "if" part) out of your business methods.

    There is also no need to implement middlewares for HTTP frameworks yourself. Most web frameworks include one by default.

    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大