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

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

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

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

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

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

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

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

Previous Articles