Go Data Type Numbers

Introduction

Go has several different types to represent numbers.

Generally, we split numbers into two different kinds: integers and floating-point numbers.

Integers

Integers are numbers without a decimal component.

For example, ..., -3, -2, -1, 0, 1, ...

Go's integer types are uint8, uint16, uint32, uint64, int8, int16, int32, and int64.

8, 16, 32, and 64 tell us how many bits each of the types use.

uint means "unsigned integer" while int means "signed integer."

Unsigned integers only contain positive numbers and zero.

There two alias types:

  • byte is the same as uint8
  • rune is the same as int32

Generally, if you are working with integers, you should just use the int type.

Floating-Point Numbers

Floating-point numbers contain a decimal component.

For example, 1.234, 123.4, 0.00001234.

Go has two floating-point types: float32 and float64.

They often referred to as single precision and double precision, respectively.

Go also has two additional types for representing complex numbers, numbers with imaginary parts:complex64 and complex128.

Generally, we should stick with float64 when working with floating-point numbers.

package main //from  ww w .  j  a va 2s  .c  om

import "fmt" 

func main() { 
    fmt.Println("1 + 1 =", 1 + 1) 
} 



PreviousNext

Related