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
+7.4k Golang : Generate Datamatrix barcode
+9.5k Golang : Roll the dice example
+28k Golang : missing Git command
+11k Golang : Send data to /dev/null a.k.a blackhole with ioutil.Discard
+9.1k Golang : Resolve domain name to IP4 and IP6 addresses.
+13.6k Golang : Adding XML attributes to xml data or use attribute to differentiate a common tag name
+9.7k Golang : Simple image viewer with Go-GTK
+17.4k Unmarshal/Load CSV record into struct in Go
+14k Golang : rune literal not terminated error
+5k Golang : Experimenting with the Rejang script
+6.3k Android Studio : AlertDialog to get user attention example
+4.1k Python : Convert(cast) bytes to string example