Declaring Variables
You declare a variable with the var keyword, followed by its name and then its type:
var umur int
var nama string
var aktif bool
A variable declared without a value automatically holds the zero value for its type:
| Type | Zero value |
|---|---|
int | 0 |
float64 | 0 |
string | "" (empty string) |
bool | false |
You can also give it a value right away:
var kota string = "Bandung"
Go doesn't allow local variables that are declared but never used — the program won't compile.
Task
A coffee shop queue app stores customer data. Declare three variables with var:
pelanggan("customer") of typestring, holding"Sari"jumlahPesanan("order count") of typeint, holding3memberof typebool, holdingtrue
The printing code is already there. Expected output:
Pelanggan: Sari
Jumlah pesanan: 3
Member: true