Let's talk a little about programming terminology. The short variable declaration :=
is used to assign a value to a variable. For example, here we assign the value 42
to the variable x
package main
import (
"fmt"
)
func main() {
x := 42
fmt.Println("Hello, playground")
}
You can only use the short declaration inside of a code block. In Go, you have to use the variable you create, so the above code will give us the error tmp/sandbox141801923/main.go:8: x declared and not used
.
If we print x, we will not have that error. We can also assign other types using the short declaration operator, such as a string
package main
import (
"fmt"
)
func main() {
x := 42
y := "James Bond"
fmt.Println(x, y)
}
Let's talk about Operators and Operands, Statements and Expressions, and Keywords.
Programming languages have keywords, also known as reserved words. These are words that the programming language uses, and cannot be used as variable names. For example, in Go, the word "package" is reserved.
If we try to use "package" as a variable name, we'll get an error: tmp/sandbox916074896/main.go:8: syntax error: unexpected package, expecting }
package main
import (
"fmt"
)
func main() {
package := 42
y := "James Bond"
fmt.Println(package, y)
}
However, variations such as "packages" can be used. It's only the exact work "package" that is a reserved keyword.
package main
import (
"fmt"
)
func main() {
packages := 42
y := "James Bond"
fmt.Println(packages, y)
}
Go Keywords
break default func interface select
case defer go map struct
chan else goto package switch
const fallthrough if range type
continue for import return var
+ & += &= && == != ( )
- | -= |= || < <= [ ]
* ^ *= ^= <- > >= { }
/ << /= <<= ++ = := , ;
% >> %= >>= -- ! ... . :
&^ &^=
You can see :=
as one of these operators. In the statement x := 42 + 7
, +
is also an operator (the addition operator). 42
and 7
are operands.
Let's talk a little bit about statements and expressions. 42 + 7
is an expression. Expressions evaluate to something. A statement is a line of code that does something. Statements usually occupy an entire line by themselves, and can include expressions. x := 42 + 7
is a statement.
"A statement is the smallest standalone element ... that expresses some action to be carried out."
"An expression ... is a combination of one or more explicit values, constants, variables, operators, and functions that the programming language interprets ... and computes to produce ... another value."
The main takeaway from this session is that there's a short declaration operator, :=
which is a common way to assign a value to a variable. You can only use it inside a code block (inside curly braces {}).
This will not work
package main
import (
"fmt"
)
x := 42
func main() {
y := "James Bond"
fmt.Println(x, y)
}
The scope is only within the function, after the declaration, so this will also throw an error, because x
and y
have not yet been declared.
package main
import (
"fmt"
)
func main() {
fmt.Println(x, y) /* x and y are not yet declared */
x := 42
y := "James Bond"
fmt.Println(x, y)
}
package main
import (
"fmt"
)
func main() {
x := 42 + 7
y := "James Bond"
fmt.Println(x)
fmt.Println(y)
x := 50
fmt.Println(x)
}
Gives the error tmp/sandbox208846163/main.go:12: no new variables on left side of :=
What that means is that x
already exists. If I change line 12 to x = 50
, this will work.
So, we have seen what the short declaration operator is. We can use it to assign a value to a new variable. We can mutate that value with the =
operator. We also learned some terminology like expressions and statements.
In the next video, we will talk about another way we can assign values to variables and declare variables, but outside of a code block.