> 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/channel.md).

# Channel

**Fungsi** -> tempat komunikasi data secara synchronous oleh go routine -> saat melakukan pengiriman data, go routine akan terblok sampai ada yang menerima data tersebut (blocking).

**Karakteristik** -> hanya bisa menerima 1 tipe data, secara default hanya bisa menampung 1 data, jika tidak digunakan harus di close atau bisa menyebabkan memory leak.

**Channel In -> Mengirim data**

**Channel Out -> Menerima data**

**Sifatnya -> Blocking**

**Channel harus di close -> jika tidak bisa menyebabkan memory leak**

## Contoh #1

```go
package main
  
import "fmt"
  
func test(value chan int) {
    // channel out
    result := 100 + <- value
    fmt.Printf("%d ", result)
}

func main() {
    fmt.Println("Start")
    value := make(chan int)

    for i := 0; i < 10; i++ {
        go test(value)
        // channel in
        value <- i
    }
    fmt.Println("\nEnd")
    close(value)
}
```

```
Start
100 101 102 103 104 105 106 107 108 109 
End
```

## Contoh 2

```go
package main
  
import "fmt"
  
func test(value chan int) {
    result := 100 + <- value
    value <- result
}

func main() {
    fmt.Println("Start")
    value := make(chan int)

    for i := 0; i < 10; i++ {
        go test(value)
        // channel in
        value <- i
        // channel out
        result := <- value
        fmt.Printf("%d ", result)
    }
    fmt.Println("\nEnd")
    close(value)
}
```

```
Start
100 101 102 103 104 105 106 107 108 109 
End
```

## Contoh 3

<pre class="language-go"><code class="lang-go"><strong>package main
</strong>  
import "fmt"

func main() {
    fmt.Println("Start")
    value := make(chan int)

    for i := 0; i &#x3C; 10; i++ {
        go func(i int) {
            result := 100 + i
            // channel in
            value &#x3C;- result
        }(i)
        // channel out
        print := &#x3C;- value
        fmt.Printf("%d ", print)
    }
    fmt.Println("\nEnd")
    close(value)
}
</code></pre>

```
Start
100 101 102 103 104 105 106 107 108 109 
End
```

## Channel perlu di close

Channel perlu di close untuk mengindikasikan tidak ada data yang dikirim lagi ke channel

Reference:

{% embed url="<https://www.scaler.com/topics/golang/closing-the-channel-in-golang/>" %}


---

# 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/channel.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.
