A basic algorithm to convert an integer number to its binary representation is to repeatedly test if the integer is even or odd (corresponding to a 0/"false" digit, or 1/"true", respectively), and dividing the number by 2 (using integer division, i.e., shifting the binary representation to the right by one position), until the number is 0.
This gives the individual bits from right to left, if you want the resulting slice to contain the bits from left to right, you need to prepend each bit to the result (see How to prepend int to slice):
func toBits (number byte) []bool {
result := []bool{}
for number > 0 {
result = append([]bool{number % 2 == 1}, result...)
number >>= 1
}
return result
}
Prepending to a slice repeatedly leads to multiple copy operations and reallocations. A more efficient solution would preallocate a slice with the target size and fill it from right to left. This would require either to calculate the size from the number (using a logarithm), or to just specify the size.