Reputation: 443
im converting some code from python to go
here i want write equal code in go lang:
python :
while g_day_no >= g_days_in_month[i] + (i == 1 and leap):
g_day_no -= g_days_in_month[i] + (i == 1 and leap)
i+=1
my try:
leap := int32(1)
var i = int32(0)
for g_day_no >= (g_days_in_month[i] + (i == 1 && leap)){
g_day_no -= g_days_in_month[i] + (i == 1 && leap)
i+=1
}
but i have error in ide that say :
Invalid operation: i == 1 && leap (mismatched types bool and int32)
for this section (i == 1 && leap)
how can i correct this part of my code?
Upvotes: 0
Views: 54
Reputation: 10517
Go is more strict about conditions. It requires booleans. leap
is an integer, so just check the value:
g_day_no >= (g_days_in_month[i] || (i == 1 && leap!=0))
More detailed answer
Booleans (True
and False
) in Python correspond to the following integer values:
True=>1
False=>0
This can be seen with the following:
>>> True+0
1
>>> False+0
0
Therefore, when you have two booleans that are being added together, its the same as an OR
:
True + True => 2 (True)
False + True => 1 (True)
True + False => 1 (True)
False + False => 0 (False)
This is the same "truth table" as OR:
True OR True => True False OR TRUE => True True OR False => True FALSE OR FALSE => False
Therefore, change your +
to an ||
(||
is OR
in Go).
Upvotes: 1