golang - compare with date

golang - compare with date
expired되었는지 아닌지 판별이 필요할 때, Golang에서는 편리하게 판별할 수 있다.
나는 timestamp로 값을 받으려고 했는데 Go에서는 date를 비교해주는 함수가 있어서 date 타입으로 받아 판별하는 구조로 server쪽을 구현했다.
now := time.Now()
expiredAt := result.ExpiredAt(db에 있는 만료기간 날짜)

if !expiredAt.After(now) {
// 오늘의 날짜와 만료기간 날짜를 비교
// true면 expiredAt > now
// false면 expiredAt < now
  c.Redirect(http.StatusTemporaryRedirect, "/expiration?url="+currentURL)
  return c.JSON(http.StatusOK, nil)
}

c.Redirect(http.StatusTemporaryRedirect, result.URL)
return c.JSON(http.StatusOK, nil)
이외에도 Before(now), Equal(now) 등등 시간관련 함수들이 많다.

댓글

가장 많이 본 글