"고루틴"의 두 판 사이의 차이

12번째 줄: 12번째 줄:


func foo(from string) {
func foo(from string) {
for i := 0; i < 3; i++ {
for i := 1; i <= 3; i++ {
fmt.Printf("from %s #%d\n", from, i)
fmt.Printf("from %s #%d\n", from, i)
time.Sleep(100*time.Millisecond)
time.Sleep(100*time.Millisecond)
27번째 줄: 27번째 줄:
<source lang='console'>
<source lang='console'>
$ go run goroutine.go  
$ go run goroutine.go  
from 직접 #0
from 직접 #1
from 직접 #1
from 직접 #2
from 직접 #2
from 직접2 #0
from 직접 #3
from 고루틴 #0
from 직접2 #1
from 고루틴 #1
from 고루틴 #1
from 직접2 #1
from 고루틴 #2
from 직접2 #2
from 직접2 #2
from 고루틴 #2
from 직접2 #3
from 고루틴 #3
</source>
</source>



2018년 6월 18일 (월) 13:34 판

1 개요

Goroutine
고루틴
  • 다른 함수 또는 메소드들을 병렬적으로 실행하는 함수 또는 메소드

2 예제

goroutine.go
package main
import "fmt"
import "time"

func foo(from string) {
	for i := 1; i <= 3; i++ {
		fmt.Printf("from %s #%d\n", from, i)
		time.Sleep(100*time.Millisecond)
	}
}

func main() {
	foo("직접")
	go foo("고루틴")
	foo("직접2")
	time.Sleep(1*time.Second)
}
$ go run goroutine.go 
from 직접 #1
from 직접 #2
from 직접 #3
from 직접2 #1
from 고루틴 #1
from 고루틴 #2
from 직접2 #2
from 직접2 #3
from 고루틴 #3

3 같이 보기

4 참고

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}