Skip to content

Commit d1cf642

Browse files
committed
Tutorial for the Golang loops.
1 parent 6ea20e1 commit d1cf642

File tree

1 file changed

+55
-1
lines changed

1 file changed

+55
-1
lines changed

README.md

+55-1
Original file line numberDiff line numberDiff line change
@@ -207,4 +207,58 @@
207207
var VariableMemoryAddress *int64 = &Variable
208208
```
209209

210-
* ### [Great explenation of pointers in go](https://www.youtube.com/watch?v=sTFJtxJXkaY&t=632s)
210+
* ### [Great explenation of pointers in go](https://www.youtube.com/watch?v=sTFJtxJXkaY&t=632s)
211+
212+
## loops
213+
* golang has a similar but different way of defining loops.
214+
* ### No `while`
215+
* Golang `for` keyword replaces the `while` keyword unlike most programming languages.
216+
```go
217+
// while (condition)
218+
for variable > 10 {
219+
}
220+
// while true
221+
for true {
222+
}
223+
```
224+
* ### Unified `foreach`
225+
* Golang has a unified method for looping elements from iterations compared to other languages.
226+
#### Go iterating array
227+
```go
228+
for i, element := range array {
229+
}
230+
```
231+
#### Go iterate map
232+
```go
233+
for key, value := range map {
234+
}
235+
```
236+
237+
#### Java iterate array
238+
```
239+
// available in java 5
240+
for (Type element : array) {
241+
}
242+
```
243+
244+
#### Java iterate map
245+
```
246+
// 1. using map entryset
247+
for (Map.Entry<String, Integer> entry : map.entrySet()) {
248+
entry.getKey();
249+
entry.getValue();
250+
}
251+
252+
// 2. using map iterator
253+
Iterator<Integer> iterator = map.values().iterator();
254+
while (iterator.hasNext()) {
255+
Integer value = iterator.next();
256+
}
257+
258+
// 3. using map foreach
259+
map.forEach((key, value) -> ...);
260+
```
261+
as you can see, Golang does have a more unified and simple method compared to java.
262+
263+
## Error Handling
264+
* golang has quite a unique way of handling errors compared to other languages.

0 commit comments

Comments
 (0)