Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert bool to int8 in golang [duplicate]

Tags:

bitSet := true var bitSetVar int8 

How can I assign bitSet to bitSetVar as 1

I can do this:

if bitSet {    bitSetVar = 1 } else {    bitSetVar = 0 } 

Is this the best way?

like image 246
Shahriar Avatar asked Jul 28 '16 04:07

Shahriar


1 Answers

Because the zero value for a int8 is 0, the else branch is not necessary.

bitSet := true var bitSetVar int8 if bitSet {    bitSetVar = 1 } 

There are no conversions from bool to integer types. The if statement is the best you can do.

like image 84
Bayta Darell Avatar answered Sep 19 '22 20:09

Bayta Darell