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
+6.1k Javascript : Get operating system and browser information
+6.7k Golang : How to determine if request or crawl is from Google robots
+8.5k Your page has meta tags in the body instead of the head
+9.2k Golang : Handle sub domain with Gin
+28.8k Get file path of temporary file in Go
+6.1k PHP : How to check if an array is empty ?
+31k Golang : Download file example
+9.1k Golang : Get SPF and DMARC from email headers to fight spam
+29.5k Golang : JQuery AJAX post data to server and send data back to client example
+13.6k Facebook PHP getUser() returns 0
+18.8k Golang : Find IP address from string
+10.7k Golang : Simple File Server