Go data types

Go is statically typed. A declaration can name a type explicitly or let the compiler infer it. Basic types include booleans, strings, integers, floating-point numbers, and complex numbers.

A simple example

Tell a number from text

  1. Start with 2 and "2"
  2. Do this Add or join
  3. Result 4 and 22
Adding two numbers gives 4. Joining two copies of the text gives the two characters 22.
main.go
package main

import "fmt"


func main() {
    fmt.Println(2 + 2)
    fmt.Println("2" + "2")
}
Output
4
22
Product label: 007. Quantity × price: 3 × 125¢. Line total: 375¢.
Keep a product code intact

A school shop sells three packs of paper at 125 cents each. Its scanner must print the code 007 exactly as it appears on the shelf. Keep the code as text and multiply the numeric quantity and price.