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
+10.1k Golang : How to use if, eq and print properly in html template
+9.4k Golang : Generate 403 Forbidden to protect a page or prevent indexing by search engine
+9.8k Golang : Removes punctuation or defined delimiter from the user's input
+7.4k Golang : Implementing class(object-oriented programming style)
+18.6k Golang : Reset or rewind io.Reader or io.Writer
+4.9k Golang : How to search a list of records or data structures
+4.6k How to check with curl if my website or the asset is gzipped ?
+3.8k JavaScript : Rounding number to decimal formats to display currency
+26.6k Get file path of temporary file in Go
+17.4k Golang : Example for RSA package functions
+9.2k Android Studio : Checkbox for user to select options example
+5k Golang : Calculate US Dollar Index (DXY)