DEV Community

loading...

Types in Go

codehakase profile image Francis Sunday ・3 min read

In the previous article, I wrote about variables and how they are used in Go. In this part, we're going to look at the Basic types available in Go.

Go is a statically typed language. This means that variables always have a specific type and that type cannot change. Static typing may seem cumbersome at first. You'll spend a large amount of your time just trying to fix your program so that it finally compiles.

The following are the basic types available in

  • Bool
  • String
  • Numeric Types
    • int8, int16, int32, int64, int
    • uint8, uint16, uint32, uint64, uint
    • float32, float64
    • complex64, complex128
    • byte
    • rune

Bool

A boolean value, is a special 1 bit integer type used to represent true and false (or on and off). Three logical operators are used with boolean values:

  • && - and
  • || - or
  • ! - not

Here is an example program showing how they can be used:

package main

import "fmt"

func main() {
  fmt.Println(true && true)
  fmt.Println(true && false)
  fmt.Println(true || true)
  fmt.Println(!false)
}

Running this program should give you:

$ go run main.go
true
false
true
true

Strings

A string is a sequence of characters with a definite length used to represent text. Go strings are made up of individual bytes, usually one for each character.

package main

import "fmt"

func main() {

  fmt.Println("Hello World \n")

  fmt.Println("Golang rocks!")
}

String literals can be created using double quotes "Hello World" or back ticks Hello World. The difference between these is that double quoted strings cannot contain newlines and they allow special escape sequences. For example \n gets replaced with a newline and \t gets replaced with a tab character.

Integers

Integers, like their mathematical counterpart, are numbers without a decimal component. (-3, -2, -1, 0, 1, …). Unlike the base-10 decimal system we use to represent numbers, computers use a base-2 binary system.

package main 

import "fmt"

func main() {

  fmt.Println("10 + 7 = ", 10 + 7)
}
$ go run main.go
10 + 7 = 17

Floating-point Numbers

Go has two floating point types: float32 and float64 (also often referred to as single precision and double precision respectively) as well as two additional types for representing complex numbers (numbers with imaginary parts): complex64 and complex128.

The following is a simple program to illustrate integer and floating point types.

package main

import (  
    "fmt"
)

func main() {  
    x, y := 2.12, 5.4

    sum := x + y
    diff := x - y

    fmt.Println("the sum: ", sum, "diff: ", diff)
}

Running the above you should see the following:

$ go run main.go
the sum:  7.5200000000000005  diff:  -3.2800000000000002

Complex Numbers

complex64: complex numbers which have float32 real and imaginary parts
complex128: complex numbers with float64 real and imaginary parts

The built-in function complex is used to construct a complex number with real and imaginary parts.

It takes a real and imaginary part as parameter and returns a complex type. Both the real and imaginary parts should be of the same type. ie either float32 or float64. If both the real and imaginary parts are float32, this function returns a complex value of type complex64. If both the real and imaginary parts are of type float64, this function returns a complex value of type complex128.

package main

import "fmt"

func main() {
  c1 := complex(2, 3)
  c2 := 2 + 47i

  cadd := c1 + c2

  fmt.Println("sum:", cadd)

  cmul := c1 * c2
  fmt.Println("product:", cmul)
}
$ go run main.go
sum: (4+50i)
product: (-137+100i)

Conclusion

In this part, we saw the basic types in Go. In the next parts of the series, we'd be using these various types interchangeably. In the next part, we'd look at Constants in Go.

I may have missed something, do me a favour and report me in the comments below or fire me a tweet @codehakase.

PS: This is the third part of the series, you refer to the first part to get a hint of the series.

Cheers!

Discussion

pic
Editor guide
Collapse
plutov profile image
Alex Pliutau

You can also mention that byte is an alias for uint8, and rune is an alias for int32 (represents a Unicode code point).

Collapse
codehakase profile image
Francis Sunday Author

That's 100% accurate Alex, thanks for pointing that out.

Collapse
nekketsuuu profile image
Takuma Ishikawa

This is not so important, but there is another basic type: uintptr.

Collapse
lordrahl90 profile image
Alugbin Abiodun Olutola

Nice tutorial esp for beginners like me...
Pls do clarify what you mean by double quoted strings doesn't allow for next line characters, i did try some and it worked.