go - 当范围超过 JSON 数组时无法迭代所有索引

标签 go

我正在创建一个独立的模拟服务器,将 JSON 文件作为集成测试的输入(请求和响应)。我只能满足第一个请求。

// MockServices ...
type MockServices struct {
    Request  string `json:"request"`
    Response string `json:"response"`
}

func main() {
r := getMockServices()

for i, s := range r {
    for i >= 0 {
        // fmt.Println(i)
        // fmt.Println(s.Request)
        http.HandleFunc(s.Request, func(w http.ResponseWriter, r *http.Request) {
            // not serving remaing endpoints
            w.Write([]byte(s.Response))
        })

        if err := http.ListenAndServe(":8080", nil); err != nil {
            panic(err)
        }
    }
}}

https://play.golang.org/p/_kFaxMnRTJJ

非常感谢任何帮助。

提前致谢

最佳答案

有很多问题:

关于go - 当范围超过 JSON 数组时无法迭代所有索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49076206/

相关文章:

post - 使用带有 map 数据的结构

database - 如何使用 bool 属性检查运行查询

go - 添加/替换/修补标准库

Go gzip 没有输出预期的输出

recursion - 嵌套循环的递归实现

loops - 在循环迭代器变量上使用 goroutines

regex - 在 Go 中获取正则表达式的解析树

Goroutines 选择范围循环

mongodb - 如何为同名的嵌套字段创建文本索引

go - 如何读取cookie?