Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a function similar to PHP's isset() in Go?

Tags:

go

In an http request, I want to know if a request contains a specific parameter, similar to PHP's isset function. How to test it in Go? Thanks.

like image 431
itczl Avatar asked Feb 06 '23 11:02

itczl


1 Answers

Once you have parsed the request, you can always check the parameter's value type to be equal to that type's zero value.

For example, you can use the "comma, ok" idiom to check query parameters:

u, err := url.Parse("http://google.com/search?q=term")
if err != nil {
    log.Fatal(err)
}
q := u.Query()
if _, ok := q["q"]; ok {
    // process q
}
like image 111
abhink Avatar answered Feb 20 '23 01:02

abhink