> For the complete documentation index, see [llms.txt](https://rafli-ramadhan.gitbook.io/golang-example-code-by-topic/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://rafli-ramadhan.gitbook.io/golang-example-code-by-topic/goroutine/go-routine-vs-synchronous.md).

# Go Routine vs Synchronous

Kodingan di bawah ini untuk menunjukkan siapa yang lebih cepat antara synchronous dan go routine. Synchronous diwakili dengan fmt.Println() tanpa go func().

Intinya yaitu go routine tidak selalu lebih cepat dari pada kode program synchronous. Go routine digunakan untuk membuat aplikasi lebih cepat, bukan membuat kodingan lebih cepat (Julizar Wira, 2023).

## Implementasi Time Sleep

```go
package main

import (
	"fmt"
	"time"
)

func main() {
    fmt.Println("start")
    duration := time.Now()

    go func() {
        fmt.Println("test 1", time.Since(duration))
        go func() {
            fmt.Println("test 1.a", time.Since(duration))
        }()
            
        go func() {
            fmt.Println("test 1.b", time.Since(duration))
        }()
    
        go func() {
            fmt.Println("test 1.c", time.Since(duration))
        }()
        
        fmt.Println("test 2", time.Since(duration))
	}()
    
    fmt.Println("end", time.Since(duration))
    time.Sleep(2*time.Second)
    fmt.Println("end", time.Since(duration))
}
```

```
start
end 14.316µs
test 1 80.584µs
test 2 118.592µs
test 1.c 128.012µs
test 1.a 143.493µs
test 1.b 156.41µs
end 2.000573491s
```

## Implementasi Sync Wait Group

```go
package main

import (
	"fmt"
	"time"
	"sync"
)

var wg sync.WaitGroup

func main() {
    fmt.Println("start")
    duration := time.Now()

    go func() {
        fmt.Println("test 1", time.Since(duration))
        
        wg.Add(3)
        go func() {
            fmt.Println("test 1.a", time.Since(duration))
        }()
            
        go func() {
            fmt.Println("test 1.b", time.Since(duration))
        }()
    
        go func() {
            fmt.Println("test 1.c", time.Since(duration))
        }()
        
        fmt.Println("test 2", time.Since(duration))
        wg.Wait()
        fmt.Println("test 3", time.Since(duration))
	}()
    
    fmt.Println("end", time.Since(duration))
    time.Sleep(2*time.Second)
    fmt.Println("end", time.Since(duration))
}
```

```
start
end 17.017µs
test 1 883.865µs
test 2 1.588318ms
test 1.c 1.636508ms
test 1.a 1.658898ms
test 1.b 1.671221ms
end 2.002663523s
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://rafli-ramadhan.gitbook.io/golang-example-code-by-topic/goroutine/go-routine-vs-synchronous.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
