douzhuang2016 2017-06-16 11:55
浏览 31
已采纳

在“ net / http”上传递* Request以使用Golang

Is it possible to pass Request value to another function?

import "net/http"

func main() {
   http.HandleFunc("saySomething", Say)
}

func Say(responseW http.ResponseWriter, request *http.Request) {
   name := getName(request) // passing request value to another function
}

func getName(request someType) string {
   request.ParseForm()
   return request.Form.Get("name")
}
  • 写回答

1条回答 默认 最新

  • doumie6223 2017-06-16 12:06
    关注

    Yes, you can 'cause request is a regular variable. It's passed by pointer, so if you will change request in getName it will change in Say too.

    package main
    
    import "net/http"
    
    func main() {
        http.HandleFunc("saySomething", Say)
    }
    
    func Say(responseW http.ResponseWriter, request *http.Request) {
        name := getName(request) // passing request value to another function
        println(name)
    }
    
    func getName(request *http.Request) string {
        request.ParseForm()
        return request.Form.Get("name")
    }
    

    See Golang tour https://tour.golang.org/moretypes/1

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?