Syntax
Entry Point
You should always have a main
function as your entry point.
Type Declarations
Type declarations always go to the right of variable or function names, e.g.
This isn't necessary in this case as it can be inferred from the right hand side
Type Inference
Go has the :=
operator which can infer the type and assign it to a variable at the same time. Using it we can write the same example as above and omit the var
and int
.
Function Declarations
Function declarations are actually quite similar (the return type is to the right of the function name), here's a function that just returns a string when called.
Here's the same version but this time it takes in a name as well
Conditionals
Conditionals are quire straightforward in Go, they're similar to most languages but without the parentheses
Loops
There's only one type of loop in Go, a for
loop.
Here's a loop that prints "Hello" five times.
Here's an infinite loop (useful for servers, game engines, etc.)
Warning: You have to kill the program to end this
This form can also be used to make a finite loop when combined with the break
keyword, this will print 0 to 4.
There's also a form of the loop that you only need to provide with the condition, this will print 0 to 5. (This is used to stop it before the next run)
Structs
Structs are a useful way to encapsulate and reuse objects, and they can even have methods. They're similar in some ways to classes in other programming languages.
Here's how to declare a struct (can be done outside of main
)
You can add methods to structs by using method receivers.
This syntax was the most confusing to me whenever I saw Go code, the (p Person)
part. This basically says that the type that can receive this method call is of type Person
and that within that function we want to name that receiver p
.
Interfaces
Interfaces add a bit of genericness to Go, they're quite similar to other languages except that you don't need to specifically declare that your struct implements an interface, the compiler infers it if the signature of your methods match your interface method's signature.
This slightly lengthy example allowed us treat both Cat
and Dog
as Eater
s in the printEatResult
function. Notice how we didn't have to do anything to explicitly say that Cats and Dogs implement the Eater interface, the compiler just figured it out because the signature of the eat
function on each of those structs matched the Eater
's version of it.
It's a convention to name interfaces that have only one method (e.g. eat
) to end with the name of that method plus er
, so eater
.
Arrays and Slices
Arrays
It's quite simple to declare and use an array (the size of the array goes to the left of the type).
However with arrays you must always declare the size, if you want to use something with flexible size, that's what slices are for.
Slices
Slices are a type built on top of arrays to provide more convenience and flexibility, most array programming in Go is done using slices.
In the example above, the size is omitted (you don't need to know the size of your data when declaring a slice)
Last updated