|
1249 | 1249 | | 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)
|
1250 | 1250 | | 14 | [What are the difference between goroutines and threads?](#what-are-the-difference-between-goroutines-and-threads)
|
1251 | 1251 | | 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) |
1253 | 1253 | | 17 | [What is a Closure?](#what-is-a-closure)
|
1254 | 1254 | | 18 | [What are runtime and runtime packages?](#what-are-runtime--and-runtime-packages)
|
1255 | 1255 | | 19 | [How can you get how many cores your computer has?](#how-can-you-get-how-many-cores-your-computer-has)
|
|
1457 | 1457 |
|
1458 | 1458 | **[ Back to Top ⬆ ](#table-of-contents---golang)**
|
1459 | 1459 |
|
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. |
1462 | 1464 |
|
1463 | 1465 | **[ Back to Top ⬆ ](#table-of-contents---golang)**
|
1464 | 1466 |
|
1465 | 1467 | 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 |
1467 | 1470 |
|
1468 | 1471 | **[ Back to Top ⬆ ](#table-of-contents---golang)**
|
1469 | 1472 |
|
1470 | 1473 | 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. |
1472 | 1476 |
|
1473 | 1477 | **[ Back to Top ⬆ ](#table-of-contents---golang)**
|
1474 | 1478 |
|
1475 | 1479 | 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 | + ``` |
1477 | 1493 |
|
1478 | 1494 | **[ Back to Top ⬆ ](#table-of-contents---golang)**
|
1479 | 1495 |
|
1480 | 1496 | 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 |
1481 | 1499 |
|
| 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 | + ``` |
1482 | 1538 |
|
1483 | 1539 | **[ Back to Top ⬆ ](#table-of-contents---golang)**
|
1484 | 1540 |
|
|
0 commit comments