# Get which Quarter of the Year in Golang

## The challenge

Given a month as an integer from 1 to 12, return to which quarter of the year it belongs as an integer number.

For example: month 2 (February), is part of the first quarter; month 6 (June), is part of the second quarter; and month 11 (November), is part of the fourth quarter.

## The solution in Golang

Option 1:

```.wp-block-code{border:0;padding:0}.wp-block-code>div{overflow:auto}.shcb-language{border:0;clip:rect(1px,1px,1px,1px);-webkit-clip-path:inset(50%);clip-path:inset(50%);height:1px;margin:-1px;overflow:hidden;padding:0;position:absolute;width:1px;word-wrap:normal;word-break:normal}.hljs{box-sizing:border-box}.hljs.shcb-code-table{display:table;width:100%}.hljs.shcb-code-table>.shcb-loc{color:inherit;display:table-row;width:100%}.hljs.shcb-code-table .shcb-loc>span{display:table-cell}.wp-block-code code.hljs:not(.shcb-wrap-lines){white-space:pre}.wp-block-code code.hljs.shcb-wrap-lines{white-space:pre-wrap}.hljs.shcb-line-numbers{border-spacing:0;counter-reset:line}.hljs.shcb-line-numbers>.shcb-loc{counter-increment:line}.hljs.shcb-line-numbers .shcb-loc>span{padding-left:.75em}.hljs.shcb-line-numbers .shcb-loc::before{border-right:1px solid #ddd;content:counter(line);display:table-cell;padding:0 .75em;text-align:right;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;white-space:nowrap;width:1%}```package solution

func QuarterOf(month int) int {
return (month + 2) / 3
}
```Code language: Go (go)```

Option 2:

``````package solution

import("math")

func QuarterOf(month int) int {
quarter := math.Ceil(float64(month)/3)
return int(quarter)
}
```Code language: Go (go)```

Option 3:

``````package solution

func QuarterOf(month int) int {
q := [12]int{1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4}
return q[month-1]
}
```Code language: Go (go)```

## Test cases to validate our solution

``````package our_test

import (
"time"
"math"
"math/rand"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("Test Example", func() {
It("should test all values", func() {
for i := 1; i < 13; i++ {
Expect(QuarterOf(i)).To(Equal(solution(i)))
}
})
It("should test random values", func() {
rand.Seed(time.Now().UnixNano())
for i := 1; i < 100; i++ {
n := rand.Intn(12)+1
Expect(QuarterOf(n)).To(Equal(solution(n)))
}
})
})

func solution(month int) int {
return int(math.Ceil(float64(month) / 3))
}
```Code language: Go (go)```
Tags:
Subscribe
Notify of