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


Function demo

Parsing common type of arguments to a function

Common parameter types

Returning errors

Returning an error

Returning multiple values from a function

Returning multiple values and print those values
Error when you do not use a value that has been captured
Replace the variable that you are not using with an underscore

Object-oriented behaviour in Go

package modelsimport (
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) {
  • 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.


Previous Articles




Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Hiring remote!

sqlcli, or: Creating a side-project in rust without knowing the language

DevOps Programmimg Language

Rocket Vault ($RVF) — Smart Vault Powered by advanced AI & ML algorithms IDO — 13-April-2021

ruby_cool_kid.rb — Meta Programming series: Ghost Methods, pulling the sheets

How to integrate africanboma API in your website using (curl)PHP Part 1

#100DaysofAWS | Day 11 | CloudFormation

Laravel define global constants Config php file

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Wasura Wattearachchi

Wasura Wattearachchi

More from Medium

Building a Simple REST API in Go With Gorilla/Mux

Getting started with GO Programming Language — Part Two

Go with gRPC, SQL, Dependency Injection(Wire) + standard project structure, and more: Introduction.

Robust media upload with Golang and Cloudinary — Echo Version