Significance of Go — Part 5 (Functions and Road to Object-Oriented Programming)

Wasura Wattearachchi
5 min readAug 23, 2020

In the previous article, we discussed different collection types in Go. This article will discuss the functions and will build the road to object-oriented programming through Go.

Functions

Like other programming languages, Go also has functions. You can write a function in Go as shown below.

Function demo

In the above screenshot, we have defined a function named hello in line 12 which takes two string arguments and prints those two strings separated by a space. The function is called in line 9 within the main() function.

Parsing common type of arguments to a function

Now again check the below function.

Common parameter types

The difference between the hello function here and in the previous example is, in here since the type of the parameters (string) is common you can specify the type only once proceeded by all the string variables (str1 and str2 ). This is an advantage of Go.

Returning errors

Like in other programming paradigms you can return values from functions in Go too. One of the special characteristics of Go is, it does not have a concept like exception handling like in Java. But Go provides a way to return errors from functions so that the calling functions can get that error and do something based on the error returned. Consider the below example.

Returning an error

As shown above, the hello function returns an error. To create a meaningful message to shown as the error, an error message was created using the package named errors, in line 10. The calling function (main) gets the error from the hello function in line 10 and prints it in line 11. (Note that, you do not need to print the error always. You can do something meaningful based on the error message you got)

Returning multiple values from a function

Go provides the feature to return multiple values from one function. Check the below example.

Returning multiple values and print those values

In the above example, three values (integer, string and error) have been returned by the function hello. The calling function has captured them using three variables as shown in line 10 and printed those values in line 11.

But there might be situations, you may not want to reuse some of the return values that have been returned by a function. Suppose you do not want to reuse the string value returned from the hello function. Check the below code segment now.

Error when you do not use a value that has been captured

You will get an error as shown above if you try to not use a variable (mystring) that has been assigned a value in Go. So to eliminate this you can do like below.

Replace the variable that you are not using with an underscore

As shown in line 10, if you replace the variable (mystring) that you are not going to reuse with an underscore you will not get that error that you received in as shown in the previous example.

Always returning an error if something goes wrong is a best practice in Go.

Object-oriented behaviour in Go

Go does not directly support the object-oriented paradigm, but we can write code in a way that supports object-oriented programming. Consider the below code segment.

package modelsimport (
"fmt"
)
type Book struct {
Title string
Sold bool
Pages int
}
func newBook() *Book {
return &Book{
Title: "Title of the book",
Sold: false,
Pages: 0,
}
}
func (b Book) printMessage(message string) {
fmt.Println(message)
}
  • In classic object oriented languages, there is a concept called a “class”. In Go, you do not have anything like that. But as shown above, you can make a struct with a name (Book) along with associated properties/attributes (Title, Sold and Pages).
  • In the above code segment, you can see a function named newBook(). This can be considered as the constructor for the class Book.
  • At the beginning of this article, we learned how to define functions in Go. In the above code segment, printMessage() is kind of a function. But actually it is a method, which was bind to the struct Book. So this is how you can create methods for a class in Go.

You can define interfaces in Go too. Please refer to the belong link to get an idea about interfaces in Go.

Conclusion

In this article, we discussed the special characteristics of the functions in Go. Also, we briefly learned how to structure our code in a way that it supports the object-oriented paradigm.

Let’s explore more about in the next article. Until then, Goodbye!

Previous Articles

--

--