Distributed Services with Go: nearestMultiple function linting error, (page 72)

Page 72 shows implementation of the nearestMultiple function. (as per below)

func​ nearestMultiple(j, k ​uint64​) ​uint64​ { ​  
​    if​ j >= 0 { ​  ​
        return​ (j / k) * k ​  
    } ​  
    ​return​ ((j - k + 1) / k) * k ​  ​  
}

This causes a listing error (go-staticcheck) in VScode: every value of type uint64 is >= 0 (SA4003)

Replace the >= with ==.

1 Like

In this case the the return value will always be 0 and we don’t need the calculation. Maybe something like this:

func​ nearestMultiple(j, k ​uint64​) ​uint64​ { ​  
​    if​ j - k >= 0 { ​  ​
        return​ (j / k) * k ​  
    } ​  
    ​return​ 0 ​  ​  
}

Am I missing something or is this functional also never called?

I gepped through the example source and only see the definitions, but it’s never used, and the static checker complains about that as well.

2 Likes