Conditional assignment

2,634 views
Skip to first unread message

llenae...@gmail.com

unread,
Apr 28, 2015, 8:30:03 AM4/28/15
to golan...@googlegroups.com
Hi there, Go (and programming in general) newbie here. I'm trying to assign the value of a variable based on a condition. I've tried 2 different ways, none of them worked: 

package main

import "fmt"

func main() {
a := 40

if a >= 35 {
title := "sir"
} else if a <= 35 {
title := "dude"
}

fmt.Printf(title)

}

This one throws the error:

# command-line-arguments

./main.go:14: undefined: title


So I know this is related to the scope of title. I've tried also with the switch statement, same result.

func main() {
a := 40

switch {
case a >= 35:
title := "sir"
case a <= 35:
title := "dude"
}

fmt.Printf(title)

}

So my question is how can I do that? meaning, how can I assign a variable based on a condition. Thanks. 

Christian von Kietzell

unread,
Apr 28, 2015, 8:42:19 AM4/28/15
to llenae...@gmail.com, golan...@googlegroups.com
You have to declare "title" before the if statement and use the '=' assignment operator:

var title string

if a >= 35 {
        title = "sir"
} else if a < 35 {
        title = "dude"
}


Chris

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

Jan Mercl

unread,
Apr 28, 2015, 8:44:34 AM4/28/15
to golan...@googlegroups.com
On Tue, Apr 28, 2015 at 2:30 PM <llenae...@gmail.com> wrote:

> So my question is how can I do that? meaning, how can I assign a 
> variable based on a condition. 

If you want to assign a value to the same variable from different scopes, use the assignment operator '='.

':=' is not an assignment operatos. It's a short variable declaration. Thus you're declaring different, although same named, variables in different scopes. (And the throwing them away which the compiler does not allow.)

Something like this might be the solution: http://play.golang.org/p/8NzTB7Z1u3

-j

llenae...@gmail.com

unread,
Apr 28, 2015, 8:45:36 AM4/28/15
to golan...@googlegroups.com, llenae...@gmail.com
Thanks a lot Christian. Problem solved. 

Carlos Castillo

unread,
Apr 28, 2015, 1:08:16 PM4/28/15
to golan...@googlegroups.com
I find that after using go for a while (and doing a lot of error checking) I tend to do the following: http://play.golang.org/p/2Gn8oIr4lh

Volker Dobler

unread,
Apr 28, 2015, 1:10:28 PM4/28/15
to golan...@googlegroups.com, llenae...@gmail.com
The problem with your code has nothing to do with "conditional assignment".

You first have to understand the difference between a short variable declaration
done with the := operator and an assignment done with =.
Doing title := "sir" declares a new variable title in the current scope (and determines
it type from the right hand side). It is equivalent to var title string; title = "sir"
which splits these two things: Declare a variable and assign a value.
Remember: In Go you cannot declare a variable and not use it.  All your
title := "sir" declare new variables an never use it in the scope declared.

Declare your variable first (var title string) and just assign it's value with title = "sir".
It dosn't matter if you use a switch or a if/else.

V.
Reply all
Reply to author
Forward
0 new messages