英文:
how to convert java annotation to go code?
问题
我正在将我的Java项目转换为Go语言。我正在使用Java注解,并希望将其转换为Go代码。我想知道在Go语言中,将该注解转换为最佳数据结构。
我的代码如下:
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
}
}
我想将注释的Java代码转换为Go代码。
如果你愿意,你也可以使用自己的示例来解释。
英文:
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
得分: 2
在你按照@icza的解释完成翻译之后,你可能希望将功能移动到一个中间件(示例),该中间件会在所有请求中运行。这样,你就可以将辅助逻辑(即“if”部分)从业务方法中分离出来。
此外,没有必要自己为HTTP框架实现中间件。大多数Web框架默认情况下都包含一个中间件。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论