-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasics.go
137 lines (112 loc) · 2.56 KB
/
basics.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"fmt"
"strings"
)
type Animal interface {
Travel() string
}
type Dog struct {
name string
breed string
}
func (dog Dog) bark() string {
return dog.name + ": Woof!"
}
func (dogBarking Dog) barkOn(dogBarked Dog) string {
return dogBarking.name + " is barking on " + dogBarked.name + "!"
}
func (dog Dog) Travel() string {
return dog.name + " is running"
}
func showNumbers(numbers ...int) (int, int) {
if len(numbers) >= 2 {
return numbers[0], numbers[1]
}
return 0, 0
}
func (dog *Dog) changeName(name string) {
dog.name = name
}
func basics() {
var i int = 999
var s string = "MyString"
var b bool = true
var u uint = 1
const pepperPl = 200
test, test2 := "Hello!", 1234
var c64 complex64 = 10 + 2i
var r rune = 'A'
var bt byte = 'a'
var f float64 = 0.000000001
fmt.Println(i, s, b, u, test, test2, pepperPl, c64, bt, r, f)
var testString string = "Test string 1234"
var p1 string = "p2"
p2 := p1
p1 = "p1"
tshp := strings.HasPrefix(testString, "Te")
tsup := strings.ToUpper(testString)
fmt.Println(len(testString), testString[0], testString[2:14], p1+" is not "+p2, tshp, tsup)
var strArr [10]string = [10]string{"a", "b", "c"}
var s2Arr = [...]string{"x", "y", "z"}
fmt.Println(strArr, s2Arr, s2Arr[1], len(s2Arr))
var slice = []string{"a", "b", "c"}
slice = append(slice, "d", "e", "f")
var slice2 []string
slice2 = append(slice2, "x", "y", "z")
var sfa []string = strArr[:2]
sfaLong := strArr[:]
fmt.Println(slice, slice2, sfa, sfaLong)
m := make(map[int]string)
m[0] = "a"
m[1] = "b"
var m2 = map[int]string{0: "a", 1: "c"}
m2[1] = "b"
delete(m2, 0)
fmt.Println(m, m2)
j := 0
for i = 0; i < 10; i++ {
j += i * 10
}
w := 0
k := 0
for k < 100 {
w += k * 2
k += 2
}
fmt.Println(j, w)
for val, num := range sfa {
fmt.Println(val, num)
}
// THIS WILL WORK WITH GO 1.22
// for val, num := range 10 {
// fmt.Println(val, num)
// }
if i < 2 {
fmt.Println(i)
} else {
fmt.Println(i)
}
switch i {
case 1:
{
fmt.Println("It's one!")
}
default:
fmt.Println("None that I know")
}
green := Dog{name: "Green", breed: "Mixed"}
pinkie := Dog{name: "Pinkie", breed: "Mixed"}
fmt.Println(green.name, green.breed)
fmt.Println(green.bark())
fmt.Println(green.barkOn(pinkie))
var emptyDog = Dog{}
var emptyDogPtr = &emptyDog
emptyDog.changeName("Lua")
fmt.Println(emptyDog, emptyDogPtr, *emptyDogPtr)
emptyDogPtr.changeName("Luna")
fmt.Println(emptyDog, emptyDogPtr, *emptyDogPtr)
var luna Animal = emptyDog
fmt.Println(luna.Travel())
fmt.Println(showNumbers(1000, 2000, 1, 2, 3, 4))
}