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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?