-
Notifications
You must be signed in to change notification settings - Fork 0
/
03_variables.go
45 lines (35 loc) · 1.61 KB
/
03_variables.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//Variables and variable definition. Static, dynamic and mixed declaration. Variable error declarion. Defining multiple variables. Constants.
package main
import "fmt"
func main() {
// syntax: var variableName variableType
// names are always written in camelCase or CamelCase (even constants)
// lowercase first letter means that variable can't be used outside the package
// var packagePrivate string
// uppercase - variable can be imported by other packages
// var CanBeImported string
// declaring a variable - memory is allocated and no value is specified by the programmer, default value is assigned
var declared int
fmt.Printf("Default value for integers is: %d\n", declared)
// defining a variable - memory is allocated and a value, specified by the programmer, is assigned
var hello string = "Hello World!"
fmt.Println(hello)
// go can infer types based on the value on the right hand side of assignemt
helloInferred := "Value on the right hand side of assignemt is of type string"
fmt.Println(helloInferred)
// assigning a new value to
helloInferred = "When we know that var has been defined, we use '=' for all following assignemts"
fmt.Println(helloInferred)
// further assignments should use '='
// uncomment the following line and run the program
// helloInferred := "helloInferred already exists"
// static type system - types don't typically change during program execution
// uncomment the following line and run the program
// helloInferred = 42
// defining multiple vars
// it is possible to declare multiple variables in a block
// var (
// variable1 string
// variable2 string
// )
}