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.4k Golang : Display list of countries and ISO codes
+22.8k Golang : Strings to lowercase and uppercase example
+42.1k Golang : How do I convert int to uint8?
+11.8k Golang : Find age or leap age from date of birth example
+17k Golang : How to generate QR codes?
+8.1k Golang : Sort words with first uppercase letter
+16.4k Golang : How to extract links from web page ?
+34.1k Golang : Proper way to set function argument default value
+32.5k Golang : Math pow(the power of x^y) example
+20.4k Golang : How to get own program name during runtime ?
+22.4k Golang : Convert seconds to minutes and remainder seconds