GoGoPrimer
< Back to module

Basic syntax

Constants and operators

A constant is declared with `const` and can never be reassigned. Its value must be computable at compile time: no function call, no result that depends on execution.

const Pi = 3.14159
const MaxRetries = 3
const AppName = "GoPrimer"

Arithmetic operators are the usual ones: `+ - * / %`. One important subtlety: division between two `int` values yields a truncated (not rounded) integer result.

fmt.Println(7 / 2) // 3, pas 3.5
fmt.Println(7 % 2) // 1 (reste de la division)
fmt.Println(7.0 / 2) // 3.5, car les opérandes sont des float64

Comparison operators (`== != < > <= >=`) return a `bool`. Logical operators are `&&` (and), `||` (or), and `!` (not), with short-circuit evaluation: `a() && b()` only calls `b()` if `a()` returns `true`.

Go performs no implicit conversion between numeric types, even close ones. `var x int32 = 1; var y int64 = 2; x + y` refuses to compile: you must convert explicitly with `int64(x) + y`.

Your turn

Declare a `VatRate` constant at 0.20 (20% VAT). Compute and print the tax-included price of a 49.90 pre-tax item.

package main
 
import "fmt"
 
func main() {
const VatRate = 0.20
priceExcludingTax := 49.90
// calcule priceIncludingTax ici
}