Let’s GO! Part 5: Functions

Ali Azizjahan
4 min readDec 20, 2021

--

One of the gophers’ main functions is to dig(x float64, y float64, length float64)

Functions were there in the programming languages almost from the very beginning. Without them there’d be no programs at all! But how do you make a function in GO? We’ll see in a bit.

Before we start, here’s some tips about the functions in GO:

  • Every program written in GO should have a function named main which will be the entry point of your application. This specific function gets no arguments at all and can’t return anything.
  • While defining a function, you have to specify the arguments, their types and the return type of the function. If no return type is provided, GO will consider that the function won’t return anything. You’ll run into errors if you try to return anything inside the body of such functions or assign their return value to a variable or something.
  • Functions in GO can return not one, not two but as many values as you want! You can return a string, an int and a float from one single function all at once and get them out in different variables as we’ll see in a few moments.

Defining a Function

Well the syntax is very easy:

func myFunctionName(argumentName1 argumentType1, argumentName2 argumentType2, ...) (returnType1, returnType2, ...) {
// the body
}

The keyword to define a function is func. Then you’ll specify your function’s name, open parenthesis, argument names followed by their type, close parenthesis, return types separated with commas inside parenthesis, open curly bracket, the body of your function and finally close curly bracket.

Note: If there’s one single return value, you shouldn’t wrap it in parenthesis.

Enough with the crap! Show me the damn code!

OK easy there fella! Here you go:

The output should be:

Dug a hole at x=1.32 y=23.543 with length of 12 meters!
Hi Gopher!

One important thing to mention here again, is that you won’t be able to capture the return value of functions inside Constants. You can only get them inside Variables. This is because the exact return value(s) of a function, even though has a known type at build time, is not know until the function actually executes at run time. With that said, it’s clear now that you’ll be working with Variables most of the times instead of Constants.

Anonymous Functions

Like most of the other languages, you can have anonymous functions in GO as well. And yes, functions are considered first class data types in GO so you can return them from another function, pass them as arguments or have them as the value of a variable or something.

Anonymous Functions are basically functions with no names. Something close to lambda functions and yes, as useful as them.

IMPORTANT NOTE: Try not to use an anonymous function as a value as long as you can because it’ll be less efficient for the compiler at the low level to evaluate a function as a value.

So let’s see them in action:

The output should be:

Hello Gopher!
Hello Gopher!
Your name is Gopher. Hello Gopher!
Your name is Gopher. How are you Gopher?

It seems a little complicated at the first glance but it’s really not. An anonymous function is simply a function without any names. Most useful for some jobs like generalization. Thus in order to use it, you either have to call it instantly after defining it (because it has no names to be called later), or assign it to a variable so it has a name and you can use it later on (not a good practice since it’s not efficient in the compile time but useful for returning a function from another function), or pass it to another function which will use it later on with the name that it has inside that specific function.

Some notes about the above code:

  • At lines 20 and 25, we’re using the function Sprintf that comes from the built-in fmt package to simply format our string with variables.
  • Note the syntax to define a function’s type at line 30. We’ll talk about it in detail later on when we reach the Reference Types part so don’t worry about it for now.
  • You’re not able to use recursion inside anonymous functions just like lambda functions since they have no names to refer back to themselves.
  • You won’t be using anonymous functions in your applications very often. Maybe only in sake of generalization or using functions that are meant to be used only once in your application some times. But it’s worthy to know how powerful GO is.

Done! That’s all you need to know about functions for now. See how easy it was?

Easy Peasy Lemon Squeezy

Let’s talk about types in the next parts.

Next:

Previous:

List Index:

--

--

Ali Azizjahan
Ali Azizjahan

Written by Ali Azizjahan

Software Engineer and Computers Researcher. Teaching you the things that I wish I knew myself sooner, or I could be taught way easier. https://linktr.ee/kyxey

Responses (1)