-
Notifications
You must be signed in to change notification settings - Fork 2
/
hello_world.go
189 lines (163 loc) · 4.04 KB
/
hello_world.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package main
import (
"fmt"
"strconv"
"math"
"sort"
"container/list"
//"strings"
)
func Product(args ...int) int {
product := 1
for i := 0; i < len(args); i++ {
product *= args[i]
}
return product
}
func Range(start int, stop int) []int {
numbers := make([]int, stop-start)
for i := start; i < stop; i++ {
numbers[i] = i
}
return numbers
}
type OrderedMap struct {
keys list.List
data map[string] int
}
func New() *OrderedMap { return new(OrderedMap) }
func (m *OrderedMap) Init() *OrderedMap {
m.keys = *list.New()
m.data = *new(map[string] int)
return m
}
func (m *OrderedMap) Insert(k string, v int) {
m.keys.PushBack(k)
m.data[k] = v
}
func main() {
fmt.Println("Hello World")
// basic math
fmt.Println("// basic math")
_sum := 0.0
_sum = 2.0 + 2.0
fmt.Println(_sum)
_sqrt := math.Sqrt(_sum)
fmt.Println(_sqrt)
// for and while loops; nested arrays; functions
fmt.Println("// loops; nested arrays; functions")
n := 10
totalsum := 0
var nsums [10][10]int
i := 0
for i < n {
for j:= n-1; j >= 0; j-- {
nsums[i][j] = Product(i,j)
totalsum += nsums[i][j]
}
i++
}
// if-conditionals
fmt.Println("// if-conditionals")
if totalsum==2025 {
fmt.Printf("totalsum*5: %d\n", totalsum*5)
} else if totalsum==0 {
fmt.Println("totalsum is zero?")
} else {
fmt.Println("totalsum is off")
}
// function overload (what is this called again?)
fmt.Println("// function overload") // not really
fmt.Println(Product(2,3,4));
// input coercion
fmt.Println("// input coercion");
ten, err := strconv.ParseInt("10", 10, 32) // TODO
_ = ten
tenpointoh, err:= strconv.ParseFloat("10.0", 64) // XXX
_ = tenpointoh
// try/except blocks
fmt.Println("// try/catch blocks")
twentypointtwo := "x20.2"
doublestr, err := strconv.ParseFloat(twentypointtwo, 64)
if err != nil {
fmt.Printf("err: %s is not a double\n", twentypointtwo)
}
doublestr, err = strconv.ParseFloat(twentypointtwo[1:], 64)
fmt.Println(doublestr)
////
// Arraylists
fmt.Println("// ArrayLists")
intlist := Range(0,5)
fmt.Println(len(intlist))
intlist = intlist[:len(intlist)-1] // pop
fmt.Println(len(intlist))
// traditional iteration
for nextint := range intlist {
fmt.Println(nextint)
}
// iteration with downcasting
for nextint := range intlist {
fmt.Println(nextint)
}
// go string join
// strform := map(strconv.Itoa, intlist)
// fmt.Println( strings.Join(intlist, "\n") ) // no upcasting
////
// HashMaps
fmt.Println("// Maps")
var _map = map[string] int {
"one": 1,
"two": 2,
"three": 3,
"four": 4,
}
fmt.Printf("size = %d\n", len(_map))
key := "one"
lookup := _map[key]
if lookup != 0 {
fmt.Printf("%s = %d\n", key, lookup)
}
searchvalue := 10
found := false
for _, value := range _map {
if value == searchvalue {
fmt.Printf("contains the value '%d'\n", searchvalue)
found = true
break
}
}
if (!found) {
fmt.Printf("does not contain the value '%d'\n", searchvalue)
}
for k,v := range _map {
fmt.Printf("%s=%d\n", k, v)
}
////
// Collections, sorting
fmt.Println("// Collections.sort")
//for k,v := range template.sortKeys(reflect.MapKeys(reflect.ValueOf(_map))) {
// fmt.Printf("%s=%d\n", k, v)
//}
keys := make([]string, len(_map))
i = 0
for k, _ := range _map {
keys[i] = k
fmt.Println(keys[i])
i++
}
sort.Strings(keys)
for i := range keys {
fmt.Printf("%s: %d\n", keys[i], _map[keys[i]])
}
////
// OrderedDict
m := New()
m.Insert("one", 1)
//fmt.Println(*m.data)
//for k,v := range (*m.data) {
// fmt.Printf("%s=%d\n", k, v)
//}
//Insert(orderedmap, "two", 2)
//Insert(orderedmap, "three", 3)
//Insert(orderedmap, "four", 4)
}