https://github.com/zkfmapf123/concurreny-pattern
concurrency pattern
https://github.com/zkfmapf123/concurreny-pattern
Last synced: 3 months ago
JSON representation
concurrency pattern
- Host: GitHub
- URL: https://github.com/zkfmapf123/concurreny-pattern
- Owner: zkfmapf123
- Created: 2024-03-01T05:21:12.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2024-09-30T10:53:37.000Z (over 1 year ago)
- Last Synced: 2025-01-13T09:38:42.076Z (over 1 year ago)
- Language: Go
- Size: 15.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Golang Concurrency Pattern
## Files
- [buffered](./buffered.go)
- [select_pattern](./select_pattern.go)
- [atomic](./atomic.go)
- [context](./main.go)
## Bufferd , Unbuffered
- Buffered - 송신자가 수신자를 기다리지 않음 (생산 > 소비)
- UnBuffered - 송신자가 수신자를 기다림...
```go
## unbuffered example
func main() {
ch := make(chan string) // 언버퍼드
go func() {
fmt.Println("고루틴: 데이터 보내는 중...")
ch <- "안녕" // 여기서 대기! (받는 쪽이 받을 때까지)
fmt.Println("고루틴: 데이터 전송 완료!")
}()
time.Sleep(3 * time.Second) // 3초 기다림
fmt.Println("메인: 이제 받을게!")
data := <-ch
fmt.Println("메인: 받은 데이터:", data)
}
고루틴: 데이터 보내는 중...
(3초 대기...)
메인: 이제 받을게!
고루틴: 데이터 전송 완료!
메인: 받은 데이터: 안녕
```
```go
## buffered example
func main() {
ch := make(chan string, 1) // 버퍼 크기 1
go func() {
fmt.Println("고루틴: 데이터 보내는 중...")
ch <- "안녕" // 버퍼에 저장하고 바로 진행!
fmt.Println("고루틴: 데이터 전송 완료!")
}()
time.Sleep(3 * time.Second) // 3초 기다림
fmt.Println("메인: 이제 받을게!")
data := <-ch
fmt.Println("메인: 받은 데이터:", data)
}
고루틴: 데이터 보내는 중...
고루틴: 데이터 전송 완료! ← 바로 완료!
(3초 대기...)
메인: 이제 받을게!
메인: 받은 데이터: 안녕
```