What is type uint16 in Golang?

Variables contain data, and data can be of different data types, or types for short. Golang is a statically typed language; this means the compiler must know the types of all the variables, either because they were explicitly indicated or because the compiler can infer the type from the code context. A type defines the set of values and the set of operations that can take place on those values.

Type uint16 in Golang is the set of all unsigned 16-bit integers. The set ranges from 0 to 65535. You should use the type uint16 when you strictly want a positive integer in the range 0-65535. Below is how you declare a variable of type uint16:

var var_name uint16

Example

package main
import "fmt"
func main() {
var var1 uint16 = 65535
fmt.Printf("Type of var1: %T", var1)
}

Explanation

The above code declares the variable var1 with type uint16, and displays its type using the Printf method from the fmt package.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved