Go: functions, methods, pointers and interfaces

In one simple example

Petr Jahoda
ITNEXT
Published in
9 min readAug 14, 2021

--

Original Gopher image created by Renee French, used Gopher image created by Maria Letta

This article was created especially for my son, who had hard times understanding using pointers and interfaces.

To make the explanation complete, I added (with his help) two things: functions and methods. I see those two as a complement to pointers and/or interfaces. Or vice versa.

This explanation is divided into six parts, each part adding onto previous part. I tried to make it all as simple as possible. If you find an error, please don’t hesitate to make a comment.

1. The basics

We will start with something very simple. We are creating two structs: Car and Truck. Both have one field name.

In a code, we will create two variables, namely bmw and volvo, with a field name. And then we will just print that variable name.

As you can see, nothing very special here. Hopefully this is the result you will see, when running the code.

bmw is the best car
volvo is the best truck

--

--