<?php...if(isset($_POST["name"])){ ...}...?>func Printuser(w http.ResponseWriter,r *http.Request){ if user,ok:=r.FormValue("user");ok{ user := r.FormValue("name") pass := r.FormValue("pass") fmt.Fprintf(w,"Hi %s - %s",user,pass) }}func Printuser(w http.ResponseWriter,r *http.Request){ if user:=r.FormValue("user");user!=""{ user = r.FormValue("name") pass := r.FormValue("pass") fmt.Fprintf(w,"Hi %s - %s",user,pass) }}func Printuser(w http.ResponseWriter,r *http.Request){ if user:=r.FormValue("user"),pass:=r.FormValue("pass");user!="" && pass!=""{ user = r.FormValue("name") pass = r.FormValue("pass") fmt.Fprintf(w,"Hi %s - %s",user,pass) }}func PrintUser(w http.ResponseWriter, r *http.Request) {
user := r.FormValue("user")
pass := r.FormValue("pass")
if user == "" || pass == "" {
fmt.Fprintf(w, "Missing username or password")
return
}
fmt.Fprintf(w, "Hi %s!", user) //I doubt you want to print the password.
}in php we can used isset function to check which variable is set or not set<?php...if(isset($_POST["name"])){...}...?>how is it in golang?
A two-value assignment tests for the existence of a key:
i, ok := m["route"]
In this statement, the first value (i) is assigned the value stored under the key "route". If that key doesn't exist, i is the value type's zero value (0). The second value (ok) is a bool that is trueif the key exists in the map, and false if not.
To test for a key without retrieving the value, use an underscore in place of the first value:
_, ok := m["route"]
I copied the above from here:
https://blog.golang.org/go-maps-in-action
Do read the full blog article to know more about the go map data type.
Regards,
Uzondu