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
+11.7k Golang : Detect user location with HTML5 geo-location
+11.5k Golang : GTK Input dialog box examples
+7.7k Golang Hello World Example
+21.2k Golang : GORM create record or insert new record into database example
+5.3k PHP : Convert CSV to JSON with YQL example
+9.3k Golang : Validate IPv6 example
+7.6k Setting $GOPATH environment variable for Unix/Linux and Windows
+12.3k Golang : Add ASCII art to command line application launching process
+16.4k Golang : Get the IPv4 and IPv6 addresses for a specific network interface
+5.3k Clean up Visual Studio For Mac installation failed disk full problem
+17.9k Golang : How to remove certain lines from a file
+24.2k Golang : Change file read or write permission example