Syntax error: favFood := "Italian" used as value

syntax error: favFood := “Italian” used as value

First of all, I don’t understand the difference between a variable and a value, though I have studied this on

Here is the code I used:

I’m trying to create a program that uses a switch statement with the switch expression specified as a variable of TYPE string with the IDENTIFIER “favFood”.

First, here’s the syntax for a switch statement: (Note, the expression must be terminated with a semicolon, ,’;’. )

Second, here’s what you’re trying to do:

Finally, if you don’t need ‘favFood’ in the switch block, just do this:

Also, string values must be enclosed in double quotes, ,’"’.

1 Like

“4” is a value. You can’t change the value of 4. A variable holds values. The value that is in a variable can change - or, if you prefer, be replaced with another value. If the variable previously held 4 and you set it to 5 you did not change the value of 4, you replaced the value in the variable.

favFood := “Italian” is a statement that declares a variable (favFood) and assigns a value (Italian) to it. That statement itself is not a value - it’s a statement.

(Other languages have other rules for what is a value or not. This is the way in Go, though.)

1 Like

Hi Cherolyn,

A variable is a thing that can be set to a value, and the value can be changed. Constants have values that cannot be changed.

var a int
a = 1    // set a to 1
fmt.Println(a)    // prints "1"
a = 2    // change the value of a to 2
fmt.Println(a)    // prints "2"

Since a was declared as an int, it can have only an integer value. The type of a is int. We set a to the value 1, then print the value of a. Then we change a's value to 2, and print that.

One very simple way to think about this is to imagine you have boxes and you want to organize stuff. You write a name on each box, then put things in the boxes. The boxes are like variables, and each has a name. The items in the boxes are like values of the variables.

An identifier is a name for a constant, variable, type, function, or other named thing in the program. Some examples:

var buyer string = "John"

In the above, “buyer” is an identifier. It refers to the variable buyer. "John" is a string. That is, the type of the variable buyer is string. (Specifically, when you see characters between double quotes, it is a literal string, as opposed to a variable of type string). “John” is a value that is being used to initialize buyer.

func main() {

Here, “main” is an identifier. It is the name of the function main().

Basically, identifiers are “words” that you use to name things. I put that in quotes because identifiers can contain digits and underscore (_) characters as well as letters. Keywords are special words in Go that are used for other things. They are not identifiers. Examples are const, var, int, string, for, and switch. You cannot use those to name things you create.

In the following,

favFood := "Italian"

favFood is an identifier (a name for a program element) and a variable. By using the := operator, we are declaring it to be of the same type as "Italian", which we are using to initialize it. So favFood is of string type, and it is set to "Italian", which is a literal string, and a value.

So you can see that different words are used to identify things in different ways. It’s a bit like how in English, the word “running” is a word, a noun, and a present participle.


Hi Cherolyn,

Here is another way to write a switch statement. I suggest you learn this because it can be very efficient and doesn’t require the computer to work as hard. Since the expression after switch is simply an integer, it just looks for a case statement with an integer that matches. In many cases, the compiler can generate code that allows the computer to get to the proper case statement by executing only a few simple machine instructions, rather than by comparing strings or performing any comparisons at all.

Also, it’s often a good idea to include a default case, even if you think it will never be executed. This is helpful for catching programming errors.


Are you talking about the blue letters? Is that the syntax? If so, I don’t know what I’m to do with that.

I see the difference in your code. Thank you.

Got the last part too. Thank you for the information.

Thank you for this explanation. It’s very helpful. I’m going to save it.

This information is very helpful, and I am going to save it. I had to work through it, and I’m sure I will need to refer to it again.

Your help with switch statements is helpful, and I will save it.
So, in “switch nationality” , nationality is an integer? I don’t understand that.

Hi Cherolyn,

Yes, nationality is an integer. The line

nationality := italian

declares nationality to be the same type as italian, which is a const. italian is defined using iota

italian = iota

And here’s the part about iota in the Go Programming Language Specification:

If you understand how iota works, you know that the actual value of italian is 0 (zero), and the other constants declared inside the parenthesis with it count upwards from there (1, 2, 3 …).

(In the specification, iota is called an untyped integer constant, which means that the specific type of integer is determined a bit later, when the value is used for something. Don’t worry if you don’t understand that yet.)

1 Like

Is ConstSpec another way of saying identifier?

c = 3 // c == 3 (iota == 2, unused)
This is interesting.
I’m not sure how to phrase the following question. Is iota in effect in a go program, even when the word is not used in the code?

ConstSpec describes a non terminal of golangs grammar. Just follow the link to get to it’s definition.

Hi Cherolyn,

Let me see if I can explain iota a little better. It’s used in const declarations when you have the consts declared in a block. First, here is a block of const declarations:

const (
    a = 0
    b = 1
    c = 2

Nothing unusual there. Compare that to this:

const (
    a = 0
    b = 0
    c = 0

They are all being set to 0. Go allows a shorter way of doing that:

const (
    a = 0

sets a, b, and c to 0. Now let’s start using iota. iota is a special constant in Go that gets incremented for each new line it’s used on (within each const declaration).

const (
    a = iota
    b = iota
    c = iota

This sets a to 0, b to 1, and c to 2. The first time you use iota, its value is 0, and it is incremented each time it is used (on a new line).

The above example is the same as

const (
    a = iota

This last one is a very common way that iota is used. Each of a, b, and c are being set to iota, but the value of iota is incremented each time it’s used. When there are only three constants, it doesn’t save much typing, but imagine if you have 20, or 100, or one for every country on the planet. It makes the code a lot easier to type and read, and may help avoid bugs.

One last thing. I said that iota is incremented with each new line in the const declaration. Well, that’s not exactly true. Actually, it is incremented for each new ConstSpec introduced in the declaration, which can be a single identifier, or a list of them. The last example given for iota in the specification shows that in use. It’s a more advanced usage, which you might not want to worry about just yet.


Thanks! I’m beginning to get it.

You don’t know how I appreciate your detailed explanations.
In your const declaration a = 0, why don’t you put it as a := 0

= is an assignment operator; it lets you assign a value to a variable (or constant) you’ve already declared. On the other hand, := is another way of declaring and assigning a variable (a sort of shorthand). const declares a constant, so the := declaration + assignment syntax is not applicable.

1 Like

Hi Cherolyn,

There are two reasons.

First, you can use a short variable declaration (with the := operator) only inside of function definitions.

Look at the error message when you try to use a short variable declaration outside of a function:

It treats it the same as an assignment statement, as if the = operator were used instead of :=. The way to do it properly is with var abc = 1. Even though both do exactly the same thing when used inside of functions, you can use only the latter form outside of functions. (Kind of odd, isn’t it?)

The other reason is that if you look carefully at the term “short variable declaration”, you can see that it’s for declaring variables, not constants.

So far, you are used to the idea that variables can change, but constants can’t. In other words, variables are “read/write”, while constants are “read only”.

This gets into a topic that is on an intermediate level, and you don’t have to worry about it or understand it just yet. When you run your programs in the Go Playground, there are actually two major phases. First, the program is compiled, and then the executable file produced by the compiler is run.

In Go, constants are handled almost entirely by the compiler and don’t even exist in a pure form by the time the program is running. For example,

const a = 1
const b = 2
var c = a + b

The value of c is set to 3 by the compiler, and is already 3 at the time the program starts running. The addition is done by the compiler, which handles the declaration of c just like

var c = 3

So the value 3 is encoded into the executable (runnable) program that the compiler generates, but the consts a and b exist only in the compiler.

Although consts and vars look like the same kind of thing when you read Go source code, they are handled differently by the Go compiler and runtime system.


I went over this information again. Helpful.

var a int
a = 1 // set a to 1
fmt.Println(a) // prints “1”
a = 2 // change the value of a to 2
fmt.Println(a) // prints “2”