Values
:= binds a name to a value. The binding is immutable:
func main() int { x := 21 return x * 2}Write x = 5 after that and the compiler stops you. To get a variable you can
assign to, say so:
func main() int { mut total := 0 for i in 0..10 { total += i } return total}This is not a style preference. Immutable data can be shared between tasks with no locking at all, and the concurrency checker leans on that. Making it the default means the common case is also the safe one.
Number literals have no type until something gives them one. 21 above became
int because nothing else claimed it. When you need a specific width, either
declare the type or convert:
func main() int { mut count i32 = 0 // declared count += 1
big := i64(count) // converted return int(big)}Sword never narrows a value for you. i64(x) is how you say you meant it.