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
+30.9k error: trying to remove "yum", which is protected
+7.5k Golang : Scanf function weird error in Windows
+18.1k Golang : Check if a directory exist or not
+7.5k Golang : Rename part of filename
+5.2k Responsive Google Adsense
+16k Golang : Get file permission
+5.6k Fix fatal error: evacuation not done in time problem
+4.8k JavaScript: Add marker function on Google Map
+4.8k MariaDB/MySQL : Form select statement or search query with Chinese characters
+10.3k Golang : Convert file unix timestamp to UTC time example
+7.7k Golang : Mapping Iban to Dunging alphabets
+22.2k Golang : Match strings by wildcard patterns with filepath.Match() function