doushan5245 2018-03-10 01:12
浏览 336
已采纳

如何在Golang中动态编写http.HandleFunc()?

I'm trying to write simple http server, that will serve requests to API. This is a code:

type Config struct {
    ListenPort int `json:"listenPort"`
    Requests   []struct {
        Request      string `json:"request"`
        ResponceFile string `json:"responceFile"`
    } `json:"requests"`
}
...

func main() {
    ...
    startServer(config)
}

func startServer(config Config) {
    http.HandleFunc(apiPrefix+config.Requests[0].Request,
        func(w http.ResponseWriter, r *http.Request) {
            var dataStruct interface{}
            err := loadJSON(config.Requests[0].ResponseFile, &dataStruct)
            if err != nil {
                w.Write([]byte("Oops! Something was wrong"))
            }
            data, _ := json.Marshal(dataStruct)
            w.Header().Set("Content-Type", "application/json")
            w.Write(data)
        })

    http.HandleFunc(apiPrefix+config.Requests[1].Request,
        func(w http.ResponseWriter, r *http.Request) {
            var dataStruct interface{}
            err := loadJSON(config.Requests[1].ResponseFile, &dataStruct)
            if err != nil {
                w.Write([]byte("Oops! Something was wrong"))
            }
            data, _ := json.Marshal(dataStruct)
            w.Header().Set("Content-Type", "application/json")
            w.Write(data)
        })

    http.HandleFunc("/", http.NotFound)

    port := ""
    if config.ListenPort != 0 {
        port = fmt.Sprintf(":%v", config.ListenPort)
    } else {
        port = ":8080"
    }

    fmt.Printf("Started @%v
", port)
    log.Fatal(http.ListenAndServe(port, nil))
}

func loadJSON(filePath string, retStruct interface{}) error {
    fmt.Println(filePath)
    fileJSON, err := ioutil.ReadFile(filePath)
    json.Unmarshal(fileJSON, retStruct)
    return err
}

And this is config, where files that should be returned via specific requests are described:

{
    "listenPort": 8080,
    "requests": [
        {
            "request": "switches/brocade",
            "responseFile": "switches.json"
        },
        {
            "request": "smth",
            "responseFile": "smth.json"
        }
    ]
}

So question is: why this code is not the same as code atop? It returns only last response file, described in config.json on all requests from this file? Or what is correct way to write dynamically-defined handlers?

func startServer(config Config) {
    for _, req := config.Requests {
        http.HandleFunc(apiPrefix+req.Request,
            func(w http.ResponseWriter, r *http.Request) {
                var dataStruct interface{}
                err := loadJSON(req.ResponseFile, &dataStruct)
                if err != nil {
                    w.Write([]byte("Oops! Something was wrong"))
                }
                data, _ := json.Marshal(dataStruct)
                w.Header().Set("Content-Type", "application/json")
                w.Write(data)
            })
    }

    http.HandleFunc("/", http.NotFound)
  • 写回答

2条回答 默认 最新

  • douaoren4402 2018-03-10 07:04
    关注

    This is because Go's range loops re-use the declared variable req.

    The iteration variables may be declared by the "range" clause using a form of short variable declaration (:=). In this case their types are set to the types of the respective iteration values and their scope is the block of the "for" statement; they are re-used in each iteration.

    (emphasis mine)

    This behaviour, together with the fact that you are capturing the variable inside a closure is the reason why all of your handlers refer to the value of the last variable.

    Function literals are closures: they may refer to variables defined in a surrounding function. Those variables are then shared between the surrounding function and the function literal, and they survive as long as they are accessible.

    To solve this you can create a new variable from the iteration variable inside the loop and have the closure use that.

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类