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
+9k Golang : Inject/embed Javascript before sending out to browser example
+32.3k Golang : Convert []string to []byte examples
+15.9k Golang : ROT47 (Caesar cipher by 47 characters) example
+9.1k Golang : automatically figure out array length(size) with three dots
+10.7k Golang : Bubble sort example
+7k Golang : How to solve "too many .rsrc sections" error?
+9.5k Golang : Web(Javascript) to server-side websocket example
+16.6k Golang : Execute terminal command to remote machine example
+9.4k Golang : How to get ECDSA curve and parameters data?
+6.6k Golang : Map within a map example
+6.9k Golang : Get expvar(export variables) to work with multiplexer
+9.1k Golang : Get SPF and DMARC from email headers to fight spam