Variables and constants in Go
Declaration forms, and formatted output.
const MaxUsers = 100
func describe() string {
name := "typestar"
var count int = 42
var ratio float64 = 3.5
ready := true
total := count * 2
return fmt.Sprintf("%s: %d/%d (%.1f) %t",
name, total, MaxUsers, ratio, ready)
}
How it works
:=infers the type;varstates it explicitly.constis fixed at compile time.fmt.Sprintfformats with%d,%.1f,%t.
Keywords and builtins used here
constfloat64funcintreturnstringvar
The run, in numbers
- Lines
- 12
- Characters to type
- 223
- Tokens
- 49
- Three-star pace
- 80 tpm
At the three-star pace of 80 tokens a minute, this run takes about 37 seconds.
Step 1 of 5 in Variables & types, step 1 of 30 in Language basics.