for
. In Go there are no while
, do
, until
keywords, you can only use for
. Which is a good thing!./repeat_test.go:6:14: undefined: Repeat
repeat_test.go:10: expected 'aaaaa' but got ''
for
syntax is very unremarkable and follows most C-like languages.{ }
are always required. You might wonder what is happening in the row:=
so far to declare and initializing variables. However, :=
is simply short hand for both steps. Here we are declaring a string
variable only. Hence, the explicit version. We can also use var
to declare functions, as we'll see later on.+=
assignment operator.+=
called "the Add AND assignment operator", adds the right operand to the left operand and assigns the result to left operand. It works with other types like integers.testing.B
gives you access to the cryptically named b.N
.b.N
times and measures how long it takes.go test -bench=.
(or if you're in Windows Powershell go test -bench="."
)136 ns/op
means is our function takes on average 136 nanoseconds to run (on my computer). Which is pretty ok! To test this it ran it 10000000 times.ExampleRepeat
to document your functionfor