Numeric types

Go's numeric types include support for integral and decimal values with a variety of sizes ranging from 8 to 64 bits. Each numeric type has its own layout in memory and is considered unique by the type system. As a way of enforcing this, and to avoid any sort of confusion when porting Go on different platforms, the name of a numeric type reflects its size requirement. For instance, type int16 indicates an integer type that uses 16 bits for internal storage. This means that numberic values must be explicitly be converted when crossing type boundaries in assignments, expressions, and operations.

The following program is not all that functional, since all values are assigned to the blank identifier. However, it illustrates all of the numeric data types supported in Go.

package main 
import ( 
   "math" 
   "unsafe" 
) 
 
var _ int8 = 12 
var _ int16 = -400 
var _ int32 = 12022 
var _ int64 = 1 << 33 
var _ int = 3 + 1415 
 
var _ uint8 = 18 
var _ uint16 = 44 
var _ uint32 = 133121 
var i uint64 = 23113233 
var _ uint = 7542 
var _ byte = 255 
var _ uintptr = unsafe.Sizeof(i) 
 
var _ float32 = 0.5772156649 
var _ float64 = math.Pi 
 
var _ complex64 = 3.5 + 2i 
var _ complex128 = -5.0i 
 
func main() { 
   fmt.Println("all types declared!") 
} 

golang.fyi/ch04/nums.go

Unsigned integer types

The following table lists all available types that can represent unsigned integers and their storage requirements in Go:

Type

Size

Description

uint8

Unsigned 8-bit

Range 0 - 255

uint16

Unsigned 16-bit

Range 0 - 65535

uint32

Unsigned 32-bit

Range 0 - 4294967295

uint64

Unsigned 64-bit

Range 0 - 18446744073709551615

uint

Implementation specific

A pre-declared type designed to represent either the 32 or 64-bit integers. As of version 1.x of Go, uint represents a 32-bit unsigned integer.

byte

Unsigned 8-bit

Alias for the unit8 type.

uintptr

Unsigned

An unsigned integer type designed to store pointers (memory addresses) for the underlying machine architecture.

Signed integer types

The following table lists all available types that can represent signed integers and their storage requirements in Go:

Type

Size

Description

int8

Signed 8-bit

Range -128 - 127

int16

Signed 16-bit

Range -32768 - 32767

int32

Signed 32-bit

Range -2147483648 - 2147483647

int64

Signed 64-bit

Range -9223372036854775808 - 9223372036854775807

int

Implementati specific

A pre-declared type designed to represent either the 32 or 64-bit integers. As of version 1.x of Go, int represents a 32-bit signed integer.

Floating point types

Go supports the following types for representation of decimal values using IEEE standards:

Type

Size

Description

float32

Signed 32-bit

IEEE-754 standard representation of single precision floating point values.

float64

Signed 64-bit

IEEE-754 standard representation of double-precision floating point values.

Complex number types

Go also supports representation of complex numbers with both imaginary and real parts as shown by the following table:

Type

Size

Description

complex64

float32

Represents complex numbers with real and imaginary parts stored as float32 values.

complex128

float64

Represents complex numbers with real and imaginary parts stored as float64 values.

Numeric literals

Go supports the natural representation of integer values using a sequence of digits with a combination of a sign and decimal point (as seen in the previous example). Optionally, Go integer literals can also represent hexadecimal and octal numbers as illustrated in the following program:

package main 
import "fmt" 

func main() { 
   vals := []int{ 
       1024, 
       0x0FF1CE, 
       0x8BADF00D, 
       0xBEEF, 
       0777, 
   } 
   for _, i := range vals { 
         if i == 0xBEEF { 
               fmt.Printf("Got %d
", i) 
               break 
         } 
   } 
} 

golang.fyi/ch04/intslit.go

Hexadecimal values are prepended with the 0x or (0X) prefix while octal values start with the number 0 as shown in the previous example. Floating point values can be represented using both decimal and exponential notations as shown in the following examples:

package main 
 
import "fmt" 
 
func main() { 
   p := 3.1415926535 
   e := .5772156649 
   x := 7.2E-5 
   y := 1.616199e-35 
   z := .416833e32 
 
   fmt.Println(p, e, x, y, z) 
} 

golang.fyi/ch04/floats.go

The previous program shows several representations of floating point literals in Go. Numbers can include an optional exponent portion indicated by e (or E) at the end of the number. For instance, 1.616199e-35 in the code represents numerical value 1.616199 x 10-35. Lastly, Go supports literals for expressing complex numbers as shown in the following example:

package main 
import "fmt" 
 
func main() { 
   a := -3.5 + 2i 
   fmt.Printf("%v
", a) 
   fmt.Printf("%+g, %+g
", real(a), imag(a)) 
} 

golang.fyi/ch04/complex.go

In the previous example, variable a is assigned a complex number with both a real and an imaginary part. The imaginary literal is a floating point number followed by the letter i. Notice that Go also offers two built-in functions, real() and imag(), to deconstruct complex numbers into their real and imaginary parts respectively.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.205.27