我想用 golang 编写一个 HTTP 代理。我使用此模块作为代理: https: //github.com/elazarl/goproxy。当有人使用我的代理时,它会调用一个以 http.Response 作为输入的函数。我们称之为“resp”。resp.Body 是一个 io.ReadCloser。我可以使用它的 Read 方法将其读入 []byte 数组。但随后它的内容就从 resp.Body 中消失了。但我必须返回一个 http.Response 以及我读入 []byte 数组的正文。我怎样才能做到这一点?
问候,
最大限度
我的代码:
proxy.OnResponse().DoFunc(func(resp *http.Response, ctx *goproxy.ProxyCtx) *http.Response {
body := resp.Body
var readBody []byte
nread, readerr := body.Read(readBody)
//the body is now empty
//and i have to return a body
//with the contents i read.
//how can i do that?
//doing return resp gives a Response with an empty body
}
Run Code Online (Sandbox Code Playgroud) 我不知道我是不是就在这里,所以如果没有,请随时删除这个问题.
我想在用Java编写的Minecraft插件中迭代块的二维平面.因此,我想要遍历每一行中的每个街区.以下是我的代码.(显然缩短了)
package mainiterator;
public class MainIterator {
public static void main(String[] args) {
int currentX = -2;
int currentZ = -2;
for (; currentX < 2; currentX++) {
for (; currentZ < 2; currentZ++) {
//The following should normally be outputted 4*4 Times. (16)
System.out.println("currentX:" + currentX + " currentZ:" + currentZ);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
但这只输出以下内容:
currentX:-2 currentZ:-2
currentX:-2 currentZ:-1
currentX:-2 currentZ:0
currentX:-2 currentZ:1
Run Code Online (Sandbox Code Playgroud)
所以有什么问题?请随意尝试.提前致谢!
问候,
Max来自德国