Golang : How do I convert int to uint8?
Problem :
You have a variable of type int and you want to convert it to type uint8.
Solution :
Type cast the variable to uint8 with uint8()
function.
package main
import (
"fmt"
"reflect"
)
var test int = 8
func main() {
fmt.Printf("The value of test is %d \n", test)
fmt.Printf("The type of test is %v \n", reflect.TypeOf(test))
newTest := uint8(test)
fmt.Printf("The value of newTest is %d \n", newTest)
fmt.Printf("The type of newTest is %v \n", reflect.TypeOf(newTest))
}
Output :
The value of test is 8
The type of test is int
The value of newTest is 8
The type of newTest is uint8
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+16.9k Golang : Covert map/slice/array to JSON or XML format
+17.4k Golang : Clone with pointer and modify value
+8.1k Golang : Check if integer is power of four example
+21.9k Fix "Failed to start php5-fpm.service: Unit php5-fpm.service is masked."
+16.3k Golang : Execute terminal command to remote machine example
+15.4k Golang : Validate hostname
+5.8k Golang : Generate multiplication table from an integer example
+5.2k Swift : Convert string array to array example
+11.6k Golang : convert(cast) float to string
+12.5k Golang : Sort and reverse sort a slice of bytes
+5.2k Javascript : Shuffle or randomize array example
+86.9k Golang : How to convert character to ASCII and back