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
+12.5k Golang : Check if an integer is negative or positive
+7.9k Golang : Apply Histogram Equalization to color images
+13.7k nginx: [emerg] unknown directive "ssl"
+6.7k Golang : Test if an input is an Armstrong number example
+7.3k Golang : How to check variable or object type during runtime?
+5.8k How to let Facebook Login button redirect to a particular URL ?
+7.4k Android Studio : Image button and button example
+2.7k Java : Get FX sentiment from website example
+18k Golang : How to get own program name during runtime ?
+7.8k Golang : Heap sort example
+21.2k Golang : Convert Unix timestamp to UTC timestamp
+9.2k Golang : How to get quoted string into another string?