Basic types

Go's basic types are

  1. bool
  2.  
  3. string
  4.  
  5. int int8 int16 int32 int64
  6. uint uint8 uint16 uint32 uint64 uintptr
  7.  
  8. byte // alias for uint8
  9.  
  10. rune // alias for int32
  11. // represents a Unicode code point
  12.  
  13. float32 float64
  14.  
  15. complex64 complex128

The example shows variables of several types, and also that variable declarations may be "factored" into blocks, as with import statements.

The int, uint, and uintptr types are usually 32 bits wide on 32-bit systems and 64 bits wide on 64-bit systems. When you need an integer value you should use int unless you have a specific reason to use a sized or unsigned integer type.

basic-types.go

  1. package main
  2. import (
  3. "fmt"
  4. "math/cmplx"
  5. )
  6. var (
  7. ToBe bool = false
  8. MaxInt uint64 = 1<<64 - 1
  9. z complex128 = cmplx.Sqrt(-5 + 12i)
  10. )
  11. func main() {
  12. fmt.Printf("Type: %T Value: %v\n", ToBe, ToBe)
  13. fmt.Printf("Type: %T Value: %v\n", MaxInt, MaxInt)
  14. fmt.Printf("Type: %T Value: %v\n", z, z)
  15. }