在“ 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")
}

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