-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
45 lines (34 loc) · 967 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package main
import (
"fmt"
"time"
)
type book struct {
title string
}
type library struct {
books []book
}
func (lib *library) addABook(book book) {
fmt.Printf("Add book '%s'\n", book.title)
lib.books = append(lib.books, book)
}
// Method below is mimicking time consuming operation.
// Method below is a Value receiver type.
func (lib *library) timeConsumingOperation() {
// `Sleep` to mimick ` time-consuming operation.
time.Sleep(30 * time.Millisecond)
// Check the number of books
fmt.Printf("Value receiver type. Found %d books.\n", len(lib.books))
}
func main() {
lib := library{}
// Goroutine mimicking a time consuming operation.
go lib.timeConsumingOperation()
// Add 2 books
go lib.addABook(book{title: "Rain Rain Go Away"})
go lib.addABook(book{title: "Rainbow After Rain"})
// `Sleep` to make sure all goroutines are completed
time.Sleep(2 * time.Second)
fmt.Printf("Actual number of books : %d books.\n", len(lib.books))
}