Skip to content

Commit b092de0

Browse files
author
Arie Bregman
authored
Merge pull request iluwatar#77 from noartem/master
Add golang questions about arrays, heap, sync
2 parents 8ff61d7 + a136ed0 commit b092de0

File tree

1 file changed

+139
-0
lines changed

1 file changed

+139
-0
lines changed

README.md

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4832,6 +4832,145 @@ func main() {
48324832
Since the first iota is declared with the value `3` (` + 3`), the next one has the value `4`
48334833
</b></details>
48344834

4835+
<details>
4836+
<summary>What will be the output of the following block of code?:
4837+
4838+
```
4839+
package main
4840+
4841+
import (
4842+
"fmt"
4843+
"sync"
4844+
"time"
4845+
)
4846+
4847+
func main() {
4848+
var wg sync.WaitGroup
4849+
4850+
wg.Add(1)
4851+
go func() {
4852+
time.Sleep(time.Second * 2)
4853+
fmt.Println("1")
4854+
wg.Done()
4855+
}()
4856+
4857+
go func() {
4858+
fmt.Println("2")
4859+
}()
4860+
4861+
wg.Wait()
4862+
fmt.Println("3")
4863+
}
4864+
```
4865+
</summary><br><b>
4866+
4867+
Output: 2 1 3
4868+
4869+
[Aritcle about sync/waitgroup](https://tutorialedge.net/golang/go-waitgroup-tutorial/)
4870+
4871+
[Golang package sync](https://golang.org/pkg/sync/)
4872+
</b></details>
4873+
4874+
<details>
4875+
<summary>What will be the output of the following block of code?:
4876+
4877+
```
4878+
package main
4879+
4880+
import (
4881+
"fmt"
4882+
)
4883+
4884+
func mod1(a []int) {
4885+
for i := range a {
4886+
a[i] = 5
4887+
}
4888+
4889+
fmt.Println("1:", a)
4890+
}
4891+
4892+
func mod2(a []int) {
4893+
a = append(a, 125) // !
4894+
4895+
for i := range a {
4896+
a[i] = 5
4897+
}
4898+
4899+
fmt.Println("2:", a)
4900+
}
4901+
4902+
func main() {
4903+
sl := []int{1, 2, 3, 4}
4904+
mod1(s1)
4905+
fmt.Println("1:", s1)
4906+
4907+
s2 := []int{1, 2, 3, 4}
4908+
mod2(s2)
4909+
fmt.Println("2:", s2)
4910+
}
4911+
```
4912+
</summary><br><b>
4913+
4914+
Output: <code><br>
4915+
1 [5 5 5 5]<br>
4916+
1 [5 5 5 5]<br>
4917+
2 [5 5 5 5 5]<br>
4918+
2 [1 2 3 4]<br>
4919+
</code>
4920+
4921+
In `mod1` a is link, and when we're using `a[i]`, we're changing `s1` value to.
4922+
But in `mod2`, `append` creats new slice, and we're changing only `a` value, not `s2`.
4923+
4924+
[Aritcle about arrays](https://golangbot.com/arrays-and-slices/),
4925+
[Blog post about `append`](https://blog.golang.org/slices)
4926+
</b></details>
4927+
4928+
<details>
4929+
<summary>What will be the output of the following block of code?:
4930+
4931+
```
4932+
package main
4933+
4934+
import (
4935+
"container/heap"
4936+
"fmt"
4937+
)
4938+
4939+
// An IntHeap is a min-heap of ints.
4940+
type IntHeap []int
4941+
4942+
func (h IntHeap) Len() int { return len(h) }
4943+
func (h IntHeap) Less(i, j int) bool { return h[i] < h[j] }
4944+
func (h IntHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
4945+
4946+
func (h *IntHeap) Push(x interface{}) {
4947+
// Push and Pop use pointer receivers because they modify the slice's length,
4948+
// not just its contents.
4949+
*h = append(*h, x.(int))
4950+
}
4951+
4952+
func (h *IntHeap) Pop() interface{} {
4953+
old := *h
4954+
n := len(old)
4955+
x := old[n-1]
4956+
*h = old[0 : n-1]
4957+
return x
4958+
}
4959+
4960+
func main() {
4961+
h := &IntHeap{4, 8, 3, 6}
4962+
heap.Init(h)
4963+
heap.Push(h, 7)
4964+
4965+
fmt.Println((*h)[0])
4966+
}
4967+
```
4968+
</summary><br><b>
4969+
4970+
Output: 3
4971+
4972+
[Golang container/heap package](https://golang.org/pkg/container/heap/)
4973+
</b></details>
48354974
## Mongo
48364975

48374976
<a name="mongo-beginner"></a>

0 commit comments

Comments
 (0)