17

It is possible to execute multiple assignment by if condition, like the following code?

func SendEmail(url, email string) (string, error) { genUri := buildUri() if err := setRedisIdentity(genUri, email); err != nil; genUrl, err := buildActivateUrl(url, genUri); { return "", err } return "test", nil } 
3
  • 3
    Doesn't that look a little hard to decipher ? Commented Aug 13, 2014 at 9:23
  • 3
    I don't think so. Your code is a bit hard to read, too. Maybe you want to split it up into multiple if-statements. Commented Aug 13, 2014 at 9:24
  • ok, I think it would be better for read too. Commented Aug 13, 2014 at 9:24

2 Answers 2

26

It looks like you want something like this:

package main import "fmt" func a(int) int { return 7 } func b(int) int { return 42 } func main() { if x, y := a(1), b(2); x > 0 && x < y { fmt.Println("sometimes") } fmt.Println("always") } 

Output:

sometimes always 
Sign up to request clarification or add additional context in comments.

Comments

14

No. Only one 'simple statement' is permitted at the beginning of an if-statement, per the spec.

The recommended approach is multiple tests which might return an error, so I think you want something like:

func SendEmail(url, email string) (string, error) { genUri := buildUri() if err := setRedisIdentity(genUri, email); err != nil { return "", err } if genUrl, err := buildActivateUrl(url, genUri); err != nil { return "", err } return "test", nil } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.