var i int = 33 // explicit type and valuevar c rune = 'a' // single quotes for runesvar d = 3.14983 // type inferred (float64)x := 10 // short declaration with inference
Go automatically zero-initializes variables:
Numbers → 0
Booleans → false
Strings → ""
Pointers, slices, maps → nil
Type Inference
The := operator infers the type from the initializer:
package mainimport "fmt"func main() { x := 42 p := &x // pointer to x fmt.Println(*p) // dereference *p = 100 fmt.Println(x) // 100}
Unlike C++, Go has no pointer arithmetic.
References
Go does not have C++-style references.
Function arguments are passed by value, but you can pass a pointer to modify the original:
func increment(n *int) { *n++}
Enumerations
Go uses iota to create enumerated constants:
type Choice intconst ( No Choice = iota Yes)var value Choice = Yesfmt.Println(value) // 1
iota auto-increments within a const block.
Arrays and Slices
Arrays have a fixed length.
Slices are dynamically sized and far more common.
var arr [3]int // array of 3 intsnums := []int{1, 2, 3} // slicenums = append(nums, 4) // add elementfmt.Println(nums[0])
Indexing starts at 0.
Strings as Byte Arrays
Strings are read-only byte slices.
To modify, convert to a slice of []rune or []byte:
s := "Go"b := []byte(s)b[0] = 'N'fmt.Println(string(b)) // "No"
Structs
A struct groups related fields:
type Car struct { RegNo string Model string Year int Mileage float64}func main() { mine := Car{RegNo: "GX123", Model: "Land Rover", Year: 2013} yours := mine fmt.Println(yours.Model)}
Constants and const / constexpr
Go’s const declares compile-time constants:
const Pi = 3.14159const BufferSize = 1024
No constexpr is needed—const already implies compile-time evaluation.
Operators in Go
Operators are symbols that perform actions on values and variables to produce a result.
Go supports:
Arithmetic
Relational (Comparison)
Logical
Bitwise
Assignment
Unary (single operand)
There is no ternary (?:) operator in Go.
Arithmetic Operators
Operator
Meaning
Example
+
Addition / unary plus
x + y, +x
-
Subtraction / unary minus
x - y, -x
*
Multiplication
x * y
/
Division
x / y
%
Modulus (remainder)
x % y
Integer division truncates toward zero.
% is only defined for integers.
a, b := 7, 3fmt.Println(a/b) // 2 (integer division)fmt.Println(a%b) // 1