Why am I getting a compile error 'cannot use ... as type uint8 in argument to ...' when the parameter is an int

The type of colorIndex is int. The argument type is uint8. An int cannot be assigned to a uint8. Here are some options for fixing the program:

  • Declare colorIndex as an untyped constant.

    const colorIndex = 2
    
  • Declare colorIndex as uint8 type:

    colorIndex := uint8(3)
    
  • Convert the value at the call:

    img.SetColorIndex(size+int(x*size+0.5), size+int(y*size+0.5), uint8(colorIndex))
    

You can replace all uses of uint8 in this answer with byte because byte is an alias for uint8.

Tags:

Go