Skip to content

Commit 1a7bbb0

Browse files
author
abregman
committed
2 parents 41c36c7 + b092de0 commit 1a7bbb0

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
@@ -4847,6 +4847,145 @@ func main() {
48474847
Since the first iota is declared with the value `3` (` + 3`), the next one has the value `4`
48484848
</b></details>
48494849

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

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

0 commit comments

Comments
 (0)