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
+5.3k Web : How to see your website from different countries?
+13.4k Golang : Set up source IP address before making HTTP request
+7.8k Golang : Embed secret text string into binary(executable) file
+7.8k Android Studio : Checkbox for user to select options example
+5.2k Golang : Transform lisp or spinal case to Pascal case example
+9.7k Golang : Clean formatting/indenting or pretty print JSON result
+13.3k Golang : How to check if input from os.Args is integer?
+5.6k Golang : File system scanning
+10k Golang : convert(cast) float to string
+5.7k Golang : Check to see if *File is a file or directory
+17.3k nginx: [emerg] unknown directive "passenger_enabled"
+18.4k Golang : Upload big file (larger than 100MB) to AWS S3 with multipart upload