Go is a static programming language. We declare variables to hold a value of a certain type. Once we declare a variable to hold a value of a certain type, it can only hold values of that type. DECLARE a VARIABLE is of a certain TYPE it can only hold VALUES of that TYPE.
So far, we have seen declaration of variables like this:
package main
import (
"fmt"
)
var z = 21
func main() {
x := 42 + 7
y := "James Bond"
fmt.Println(x)
fmt.Println(y)
x = 50
fmt.Println(x)
fmt.Println(z) /* z is in scope here as well */
}
func foo() {
fmt.Println(z) /* z is in scope anywhere throughout the package */
}
With short declaration operator or declaring a variable with the var keyword, the compiler can figure out the type of the value that is being asigned for us.
Ease of programming is one of the things the designers of Go wanted to build into this language. Efficient execution, meaning it's performant, efficient compilation, meaning you don't have to wait forever for your program to be built, and ease of programming, making it easy. This is the best of all worlds.
You can also be more specific, instead of var z = 21
, you can say var z int = 21
.
package main
import (
"fmt"
)
var z int = 21
func main() {
fmt.Println(z)
}
We'll explore all the types we can work with in an upcoming section.
You can also declare the var z as type in, then assign a value to it later,
package main
import (
"fmt"
)
var z int
func main() {
z = 21
fmt.Println(z)
}
You can declare the variable z
as type int, but if you do that without assigning a value to it outside of the codeblock, you cannot also assign it a value outside the codeblock, e.g/
package main
import (
"fmt"
)
var z int
z = 21
func main() {
fmt.Println(z)
}
tmp/sandbox147413757/main.go:8: syntax error: non-declaration statement outside function body
You can only have declaration statements outside of a function body.
[
There are two terms you should know; primitive data types and composite data types.
Primitive data type: either a basic type provided by a programming language as a basic building block or a data type for which the programming language provides built-in support. You can think of basic types as something like int
, string
, bool
, or you can think of it as a built-in type, in which case that would also encompass composite types. So basically a primitive is provided by the language and is a composite data type or it is provided by the language and is not a composite data type.
A composite data type is going to allow you to compose together or aggregate together other values of a certain type. You have primitive data types, provided by the programming language, which may or may not include composite data types, and you have composite data types, which is taking a bunch of basic data types, like int
, string
, bool
and composing them together in some kind of aggregate data structure, and that's a composite of all the other values of this type.
Primitive data types might also include slice
, array
, structure
, struct
, and a composite data type is a data type that holds other data types.
To recap, we can DECLARE a VARIABLE is of a certain TYPE it can only hold VALUES of that TYPE. We can specify the type of a variable during declaration var z int = 21
or if you want to assign a value after you have declared the variable, you can do so within a function.
package main
import (
"fmt"
)
var z int
func main() {
z = 21
fmt.Println(z)
}
We also talked about primitive data types and composite data types. A primitive data type is just a data type that is provided by the language, for example string
, bool
, int
, it could also include composite data types, because if it's already built into the language, it can be considered a primitive. And, composite data types allow you to bring together multiple values of different types and hold them in one data structure, so you're composing together a bunch of values of different types. When you do that, it's called composition.