티스토리 뷰

golang

time.Sleep, time.Duration

주먹불끈 2019. 2. 11. 17:29

개요

 

time.Sleep 시에 나오는 time.Duration 이해해보자

 

참고링크: https://www.ardanlabs.com/blog/2013/06/gos-duration-type-unravelled.html

참고링크: https://stackoverflow.com/a/41503910/6513756

 

 

궁금증

 

Playground: https://play.golang.org/p/vuHqO4Jxr04


time.Sleep(time.Duration(2) * time.Millisecond) 2ms 동안 sleep 하게 되는데

timeout := 2

time.Sleep(time.Duration(2) * time.Millisecond) 에러가 난다.

 


 

time.Duration

 

time package Duration int64 custom type 이다.

따라서, Nanosecond, Microsecond, Millisecond, Second, Minute, Hour 모두 Duration type 이다.

그리고 time.Sleep() Duration type 받는다.

 


 

 

2 timeout 차이

 

궁금증 항목의 2 untyped constant (=adaptive type) 이며, 따라서 자동으로 time.Duration type (= int64 type) 으로 convert 된다.

하지만 timeout := 2 경우 int type 이다.

 

Golang 경우 multiply 같은 type 끼리만 된다.

 


반응형
반응형
잡학툰 뱃지
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/03   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
글 보관함