1. 기본구조
수행시 go 키워드를 붙여서 실행시키면 동시에 실행됨.
package main
import (
"fmt"
"time"
)
func t1() {
time.Sleep(time.Second * 2)
fmt.Println("t1 func...")
}
func main() {
go t1()
fmt.Println("this is main func...")
time.Sleep(time.Second * 3)
}
2. 테스트 예제
package main
import (
"fmt"
"net/http"
"os"
"time"
)
func responseTime(url string) {
start := time.Now()
res, err := http.Get(url)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer res.Body.Close()
elapsed := time.Since(start).Seconds()
fmt.Printf("%s took %v seconds \n", url, elapsed)
}
func main() {
urls := make([]string, 3)
urls[0] = "https://www.naver.com/"
urls[1] = "https://www.google.com/"
urls[2] = "https://www.apple.com/"
for _, u := range urls {
go responseTime(u)
}
time.Sleep(time.Second * 5)
}
'Language > go' 카테고리의 다른 글
json 코드 (0) | 2018.03.15 |
---|---|
http client 코드 (0) | 2018.03.15 |
http 서버 코드 (0) | 2018.03.15 |
go error handling (0) | 2018.01.02 |
go type default 값 (0) | 2018.01.01 |