Go for Beginners Part 3 - Functions in Go

Go for Beginners Part 3 - Functions in Go

Hello!

This is a post from the Golang Series: Go Language for Beginners in 16 Parts!

In this post we're going to discuss a little bit about Functions in Go!

Declaring Functions in Go

It's really simple to declaring a new function in Go. The pattern is:

Let's try it out:

Declaring Functions that Receives Arguments

As you can imagine, Go Functions can receive arguments:

As you can see, the pattern is:

Tip: If your arguments are using the same type, you can declare the type just once:

Returning Values from Functions in Go

Of course we can return values from functions in Go.

We just need to follow the pattern:

So, in order to return the total value, let's create the code below:

As you can see, we're returning the value from the sum() function and assigning its value to the variable value

I know, if your coming from the Ruby World, you are sad. Go doesn't return the last statement in the function as Ruby does

Returning a Value from a Function with Implicit Variable

In Go you can explicit declare a variable that should be used to return the value and in the return statement you can omit the variable, returning the value implicitly

The pattern is:

Notice that the return statement could return the yourVariable explicitly:

The complete code could be:

Returning Multiple Values from Functions in Go

You can return multiple values from a function:

You could return more than 2:

Variadic Arguments in Go Functions

Variadic Functions can be called with many arguments, zero to N that can be varied!

The pattern is:

Let's see some code:

With variadic functions in Go you can pass zero arguments:

Great! In the next post: Part 4 - Error Handling in Go we're going to see how to use Error Handling in Go!

That's it! I hope that was useful to you! Thank you 🙂

Don't forget to see and fork the project on GitHub!

Follow us to keep up to date! \o/

Alexandre Gama

Alexandre Gama

Hacking Code Founder

Hacking Code Founder and Writer, Passionate Senior Software Engineer and Team Leader at @Elo7, Teacher of several courses at Caelum, Speaker at many conferences and terrible guitar player.
Go Language Tutorial for Beginners - Go Lang Characteristics - Hacking Code

Part 1 - Go Language Characteristics

This is the first Post of the complete series about Go Language!

Go Language Tutorial for Beginners - Declaring Variables in Go - Hacking Code

Part 2 - Declaring Variables in Go

How can we declare and use variables in Go? Check it out!

Go Language Tutorial for Beginners - Functions in Go - Hacking Code

Part 3 - Functions in Go

Do you want to try a more Functional Programming Style with Functions?

Go Language Tutorial for Beginners - Error Handling in Go - Hacking Code

Part 4 - Error Handling in Go

Errors always happen! How can we handle Errors in Go Language?

Go Language Tutorial for Beginners - Flow Control in Go - Hacking Code

Part 5 - Flow Control in Go

How to live without a Flow Control in Go? We can’t! Let’s see how to use it!

Go Language Tutorial for Beginners - Conditionals in Go - Hacking Code

Part 6 - Conditionals in Go

In this post we’re going to see how to work with Conditionals in Go!

Go Language Tutorial for Beginners - Switch in Go - Hacking Code

Part 7 - Switch in Go

Do you like to have many if statement in you code? So, let’s see switch statement in Go!

Go Language Tutorial for Beginners - Arrays in Go - Hacking Code

Part 8 - Arrays in Go

Data Structures in Go are really common! In this post we’re going to see Arrays in Go!

Go Language Tutorial for Beginners - Slices in Go - Hacking Code

Part 9 - Slices in Go

Slices in Go are really important and used in every code! Let’s learn about it!

Go Language Tutorial for Beginners - Maps in Go - Hacking Code

Part 10 - Maps in Go

What about using Maps in Go?

Go Language Tutorial for Beginners - Custom Types in Go - Hacking Code

Part 11 - Custom Types in Go

Let’s learn how to create and user Custom Types in Go

Go Language Tutorial for Beginners - Structs in Go - Hacking Code

Part 12 - Structs in Go

Structs in Go allow us to have types of an abstraction together! Let’s see how!

Go Language Tutorial for Beginners - Methods in Go - Hacking Code

Part 13 - Methods in Go

Methods in Go are created a little bit different from other Languages. Let’s see how?

Go Language Tutorial for Beginners - Interfaces in Go - Hacking Code

Part 14 - Interfaces in Go

Interfaces in Go are really different but exciting feature! Let’s see why!

Go Language Tutorial for Beginners - Concurrency with Goroutines in Go - Hacking Code

Part 15 - Concurrency with Goroutines in Go

Concurrency in Go is fantastic and simple! We’re going to see Goroutines now!

Go Language Tutorial for Beginners - Concurrency with Channels in Go - Hacking Code

Part 16 - Concurrency with Channels in Go

We can’t live with Goroutines without Channels when working with Concurrency in Go! Let’s see why?

0 Comments

Leave a Reply

Share This
Subscribe To Our Newsletter

Subscribe To Our Newsletter

Join our mailing list to receive the latest news and updates from our team.

You have Successfully Subscribed!

%d bloggers like this: