isset in golang!

7,387 views
Skip to first unread message

amir-taghavi

unread,
Dec 1, 2014, 11:35:48 AM12/1/14
to golan...@googlegroups.com
hi
 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?

Matthew Kane

unread,
Dec 1, 2014, 11:39:43 AM12/1/14
to amir-taghavi, golang-nuts
If you're looking to see if a key exists in a map, like the PHP code
here is doing, use this form of map indexing:

if name, ok := yourmap["name"]; ok {
// name is the string that was stored in the map
} else {
// name is the zero value of whatever values are stored in the map
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
matt kane's brain
im: mkb.di...@gmail.com (gtalk) / mkbatwerk (AIM)
twitter: the_real_mkb / nynexrepublic
http://hydrogenproject.com

amir-taghavi

unread,
Dec 1, 2014, 12:00:19 PM12/1/14
to golan...@googlegroups.com
please see the follow code 
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)
}
}

i don't underestand which how isset r.FormValue("user") please help me

cm...@golang.org

unread,
Dec 1, 2014, 12:08:32 PM12/1/14
to golan...@googlegroups.com
First of all, does that code even compile? FormValue (http://godoc.org/net/http#Request.FormValue) only returns a string and that string will be empty if the key is not found. If you're trying to replicate the behavior of isset, you probably want something like if user := r.FormValue("user"); user != "" { ... }

Secondly, you probably want to use PostFormValue (http://godoc.org/net/http#Request.PostFormValue) since in your previous example, you only want to read from POST. Using FormValue is like saying if (isset($_POST["user"]) || isset($_GET["user"])) { ... }.

amir-taghavi

unread,
Dec 1, 2014, 12:17:18 PM12/1/14
to golan...@googlegroups.com
thanks my problem solved
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)
}
}

and one other question

if i want check 2 parameters example follow
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)
}
}


how is it possible ? 

cm...@golang.org

unread,
Dec 1, 2014, 12:29:05 PM12/1/14
to golan...@googlegroups.com
You can do multiple assignments in one line with the comma operator e.g. user, pass := r.FormValue("user"), r.FormValue("pass"), but that doesn't seem very idiomatic. Just initialize the variables and then check them:

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.
}

amir-taghavi

unread,
Dec 1, 2014, 12:38:36 PM12/1/14
to golan...@googlegroups.com
very thanks dude

asadhay...@gmail.com

unread,
Jul 18, 2018, 8:48:21 AM7/18/18
to golang-nuts
It will be a compile time error in go if you use a variable that is not declared ...

Uzondu Enudeme

unread,
Jul 21, 2018, 9:09:13 AM7/21/18
to asadhay...@gmail.com, golang-nuts
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

Reply all
Reply to author
Forward
0 new messages