Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fb358d5

Browse files
adding 5 new questions
1 parent 43bbdd5 commit fb358d5

File tree

1 file changed

+62
-6
lines changed

1 file changed

+62
-6
lines changed

‎README.md

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,7 +1249,7 @@
12491249
| 13 | [What are concurrency and parralism and what is the difference between both?](#what-are-concurrency-and-parralism-and-what-is-the-difference-between-both)
12501250
| 14 | [What are the difference between goroutines and threads?](#what-are-the-difference-between-goroutines-and-threads)
12511251
| 15 | [What are channels for?](#what-are-channels-for)
1252-
| 16 | [Can you do something in goroutines without channels?](#can-you-do-something-in-goroutines-without-channels)
1252+
| 16 | [Can you do something in goroutines using channels?](#can-you-do-something-in-goroutines-using-channels)
12531253
| 17 | [What is a Closure?](#what-is-a-closure)
12541254
| 18 | [What are runtime and runtime packages?](#what-are-runtime--and-runtime-packages)
12551255
| 19 | [How can you get how many cores your computer has?](#how-can-you-get-how-many-cores-your-computer-has)
@@ -1457,28 +1457,84 @@
14571457

14581458
**[ Back to Top ⬆ ](#table-of-contents---golang)**
14591459

1460-
16. ### Can you do something in goroutines without channels?
1461-
1460+
16. ### Can you do something in goroutines using channels?
1461+
* Channels are goroutine-safe and can store and pass values between goroutines
1462+
* Channels provide FIFO semantics.
1463+
* Channels cause goroutines to block and unblock, which we just learned about.
14621464

14631465
**[ Back to Top ⬆ ](#table-of-contents---golang)**
14641466

14651467
17. ### What is a Closure?
1466-
1468+
1469+
A closure is a function value that references variables from outside its body. The function may access and assign to the referenced variables
14671470

14681471
**[ Back to Top ⬆ ](#table-of-contents---golang)**
14691472

14701473
18. ### What are runtime and runtime packages?
1471-
1474+
1475+
The runtime library implements garbage collection, concurrency, stack management, and other critical features of the Go language. The Package runtime contains operations that interact with Go's runtime system, such as functions to control goroutines.
14721476
14731477
**[ Back to Top ⬆ ](#table-of-contents---golang)**
14741478
14751479
19. ### How can you get how many cores your computer has?
1476-
1480+
1481+
```go
1482+
package main
1483+
1484+
import (
1485+
"fmt"
1486+
"runtime"
1487+
)
1488+
1489+
func main() {
1490+
fmt.Println(runtime.NumCPU())
1491+
}
1492+
```
14771493
14781494
**[ Back to Top ⬆ ](#table-of-contents---golang)**
14791495
14801496
20. ### How would you tell a goroutine to use less core than what you have?
1497+
1498+
We can restrict the number of goroutines running at the same time , like below
14811499
1500+
```go
1501+
package main
1502+
1503+
import (
1504+
"flag"
1505+
"fmt"
1506+
"time"
1507+
"sync"
1508+
)
1509+
1510+
// Fake a long and difficult work.
1511+
func DoWork() {
1512+
time.Sleep(500 * time.Millisecond)
1513+
}
1514+
1515+
func main() {
1516+
maxNbConcurrentGoroutines := flag.Int("maxNbConcurrentGoroutines", 2, "the number of goroutines that are allowed to run concurrently")
1517+
nbJobs := flag.Int("nbJobs", 5, "the number of jobs that we need to do")
1518+
flag.Parse()
1519+
1520+
concurrentGoroutines := make(chan struct{}, *maxNbConcurrentGoroutines)
1521+
1522+
var wg sync.WaitGroup
1523+
1524+
for i := 0; i < *nbJobs; i++ {
1525+
wg.Add(1)
1526+
go func(i int) {
1527+
defer wg.Done()
1528+
concurrentGoroutines <- struct{}{}
1529+
fmt.Println("doing", i)
1530+
DoWork()
1531+
fmt.Println("finished", i)
1532+
<-concurrentGoroutines
1533+
}(i)
1534+
}
1535+
wg.Wait()
1536+
}
1537+
```
14821538
14831539
**[ Back to Top ⬆ ](#table-of-contents---golang)**
14841540

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /