Basics
Go variables, types, functions, and scope.
Go uses camelCase by convention. Variables are statically typed, and the := operator infers types.
Comments
// Single-line comment
/* Multi-line comment */
Variables
// Short declaration (inside functions)
name := "Ada" // string
age := 30 // int
price := 9.99 // float64
active := true // bool
// var declaration (package or function level)
var name string = "Ada"
var count int
Zero Values
Uninitialized variables get zero values: 0 for ints, "" for strings, false for bools, nil for pointers/slices/maps/channels.
Constants
const Pi = 3.14159
const (
StatusOK = 200
StatusError = 500
)
Functions
func greet(name string) string {
return "Hello, " + name
}
// Multiple return values
func divide(a, b int) (int, error) {
if b == 0 {
return 0, errors.New("division by zero")
}
return a / b, nil
}
// Named returns
func coords() (x, y int) {
x, y = 10, 20
return
}
Scope
- Package level —
varandfuncdeclarations visible within the package - Function level —
:=creates function-scoped variables - Block level — variables inside
{}blocks
Next: Conditions & Switches