-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
58 lines (45 loc) · 1.16 KB
/
main.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
46
47
48
49
50
51
52
53
54
55
56
57
58
package variables
import "fmt"
// Different ways to assign variables in golang
const (
/*
Basic constant values. Constants are evaluated at compile time so setting them
to computed dynamic value(s) is not allowed. See the commented code below.
*/
A = 1
B = 2
C = 3
// D = computedValue(5) <- This is not allowed as it cannot be evaluated during compilation.
)
func computedValue(n int) int {
return n + 1
}
type Object struct {
x int
}
func Run() {
fmt.Println(A, B, C)
// Declaring multiple variables on the same line
var a, b, c string = "a", "b", "c"
fmt.Println(a, b, c)
// Have go infer the types automatically
d, e, f := "d", "e", "f"
fmt.Println(d, e, f)
// Variables without an initialization value are zero-valued
// In the case of objects and pointers, these are `nil`
// `nil` is gos equivalent to `None` or `Null` in other languages.
var myObject Object
fmt.Println(myObject, myObject.x)
// zero valued strings are empty ""
var foo string
fmt.Println(foo)
// zero valued ints and floats are 0
var (
one int
two float64
)
fmt.Println(one, two)
// zero valued booleans are false
var predicate bool
fmt.Println(predicate)
}