Home > front end >  Golang get mux value
Golang get mux value

Time:06-24

I'm new to go. I'm using mux and trying to parse out a url parameter. I have the following code where r is a *http.Request

vars := mux.Vars(r)
user := vars["user"]
fmt.Print(user)

mux grabs the whole url parameter string like the following print out.

"user=username"

I'm wondering if it's possible to simply grab the value (username), or if I need to do regex to get the username.

Thanks for any help!!

CodePudding user response:

Mux's Vars attribute is more for parsing path variables such as user/{id} and being able to extract out that information. For actual URL query parameters, you're better off using the built-in request Query() object.

user := r.URL.Query().Get("user")
  •  Tags:  
  • go
  • Related