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 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line