In the previous tutorial, we began by writing a Hello World program in Google Go. This tutorial will look into the available primitive data types and how to declare and define variables in Go programming.
Basic Data Types
The following are the basic data types that are available in the Google Go programming language.
bool boolean truth values of either true or false uint8 the set of all unsigned 8-bit integers (0 to 255) uint16 the set of all unsigned 16-bit integers (0 to 65535) uint32 the set of all unsigned 32-bit integers (0 to 4294967295) uint64 the set of all unsigned 64-bit integers (0 to 18446744073709551615) int8 the set of all signed 8-bit integers (-128 to 127) int16 the set of all signed 16-bit integers (-32768 to 32767) int32 the set of all signed 32-bit integers (-2147483648 to 2147483647) int64 the set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807) float32 the set of all IEEE-754 32-bit floating-point numbers float64 the set of all IEEE-754 64-bit floating-point numbers complex64 the set of all complex numbers with float32 real and imaginary parts complex128 the set of all complex numbers with float64 real and imaginary parts byte familiar alias for uint8 uint either 32 or 64 bits int either 32 or 64 bits float either 32 or 64 bits string represents the set of string values
To avoid portability issues all numeric types are distinct except byte, which is an alias for uint8. Conversions are required when different numeric types are mixed in an expression or assignment. For instance, int32 and int are not the same type even though they may have the same size on a particular architecture.
Variables
A computer variable can represent any kind of data that can be stored in a computer system.
Variables in the Go programming language can be declared as,
var s string = ""
This is the var keyword, followed by the name of the variable, followed by its type, followed by an equals sign and an initial value for the variable.
Go tries to be terse, and this declaration could be shortened. Since the string constant is of type string, we don’t have to tell the compiler that. We could write
var s = ""
or we could go even shorter and write the idiom
s := ""
Following are several example of declaring different types of variables in Go.
var i int
var U, V, W float
var k = 0
var x, y float = -1, -2
var (
i int
u, v, s = 2.0, 3.0, "bar"
)
If no initial value is given to a variable, then that variable is initialized to it’s zero value. False for booleans, 0 for integers, 0.0 for floats, “” for strings, and nil for pointers, functions, interfaces, slices, channels, and maps.
Type Conversions
Go does not support implicit type conversion. To convert a numeric value from one type to another is a conversion, with syntax like a function call:
uint8(int_var) // truncate to size int(float_var) // truncate fraction float64(int_var) // convert to float
Also some conversions to string:
string(0x1234) // == "u1234" string(array_of_bytes) // bytes -> bytes string(array_of_ints) // ints -> Unicode/UTF-8
Related Posts:
Tags: google go

