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
+21.6k Golang : Strings to lowercase and uppercase example
+24.6k Mac/Linux and Golang : Fix bind: address already in use error
+6k Linux : How to fix Brother HL-1110 printing blank page problem
+11.3k Golang : Sort and reverse sort a slice of bytes
+16.5k Golang : How to log each HTTP request to your web server?
+8.7k Golang : Identifying Golang HTTP client request
+14.1k Golang : How do I get the local IP (non-loopback) address ?
+29.3k Golang : Download file example
+18.2k Golang : Compare floating-point numbers
+8.8k Golang : Translate language with language package example
+6.6k Setting $GOPATH environment variable for Unix/Linux and Windows
+10.4k Get form post value in Go