티스토리 뷰
개요
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 끼리만 된다.
'golang' 카테고리의 다른 글
| slack slash command 는 무얼 보내주는 걸까 (0) | 2019.02.15 |
|---|---|
| Slack slash command + Golang server (0) | 2019.02.12 |
| Golang: Github - Travis-CI - Heroku (0) | 2019.02.07 |
| Go Modules (0) | 2019.02.07 |
| Golang: io.ReadWriter 의 content 를 날려먹지 않는 법 (1) | 2019.01.28 |
- Total
- Today
- Yesterday
- 인텔리제이
- go
- gocore
- strange
- intellij
- agile
- API
- bun
- 티스토리챌린지
- notion
- websocket
- 잡학툰
- middleware
- 오블완
- 독서
- 독서후기
- 클린 애자일
- solid
- ChatGPT
- OpenAI
- 영화
- github
- MCP
- Gin
- Echo
- golang
- backend
- clean agile
- 체호프
- postgres
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
